1、常用的模块 os,sys,time,datetime、hashlib
os 导入OS,
import os
os.remove() #删除文件 os.rename(old,new) #重命名 print(os.sep)#当前操作系统的路径分隔符 # # res = os.system('ipconfig') #执行操作系统命令的,但是获取不到结果 #res = os.popen('ipconfig').read() #可以获取到命令执行的结果 # print(os.path.abspath(__file__))#获取绝对路径 #print(os.path.dirname("e:\\syz\\ly-code"))#获取父目录,获取它的上一级目录 # print(os.path.exists(r"E:\syz\ly-code\day6")) #判断文件或者目录是否存在 print(os.path.isfile("xiaohei.py")) #判断是否是一个文件,1、文件要存在2、必须是一个文件 print(os.path.isdir("e:\\syz1"))#是否是一个路径,目录是否存在 size = os.path.getsize('x.py') #获取文件的大小 os.path.join("root",'hehe','mysql','a.sql') #拼接路径 for abs_path,dir,file in os.walk(r'e:\nhy'): print(abs_path,dir,file) # abs_path 当前循环的绝对路径 # dir 目录下面所有的文件夹 [ ] # file 目录下面的所有文件 []
# 把双数日期的日志,里面给随便写点东西。 # 1、获取到log目录下面的所有文件 os.walk() # 2、根据文件名来判断,是否是双数日期 ,分割字符串,取到日期 # 3、12%2==0 # 4、打开这个文件 open() import os for abs_path,dir,file in os.walk(r'E:\syz\ly-code\day6\logs'): for f in file: day = f.split('.')[0].split('-')[-1] if int(day)%2==0: file_name = os.path.join(abs_path,f)#拼接绝对路径 open(file_name,'a+',encoding='utf-8').write('写东西')#
sys 系统信息
import sys # print(sys.platform) #判断操作系统 # #python的环境变量 # sys.path.append(r'E:\syz\ly-code\day5') # sys.path.insert(0,r'E:\syz\ly-code\day5') # print(sys.path) # import nhy # nhy.my() # print(nhy.name) # import nhy # nhy.my() # print(nhy.name) print(sys.argv) #用来获取命令行里面运行python文件的时候传入的参数
time 时间模块
import time
#1、时间戳 从unix元年到现在过了多少秒 #2、格式化好的时间 #先转成时间元组 # print(time.time()) #获取当前时间戳 # time.sleep(10) today = time.strftime('%Y%m%d%H%M%S') # print(today) # print(time.gmtime()) #默认取的是标准时区的时间 s=time.localtime(1514198608) #取到的是当前时区的时间 # print(time.strftime('%Y-%m-%d %H:%M:%S',s)) #时间戳转换时间元组 # 1、时间戳转成时间元组 time.localtime() # 2、再把时间元组转成格式化的时间 def timestamp_to_fomat(timestamp=None,format='%Y-%m-%d'): #1、默认返回当前格式化好的时间 #2、传入时间戳的话,把时间戳转换成格式化好的时间,返回 if timestamp: time_tuple = time.localtime(timestamp) res = time.strftime(format,time_tuple) else: res = time.strftime(format) #默认取当前时间 return res # 2018-4-21 # tp = time.strptime('2018-4-21','%Y-%m-%d') #把格式化好的时间转成时间元组的 # print(time.mktime(tp)) #把时间元组转成时间戳 def strToTimestamp(str=None,format='%Y%m%d%H%M%S'): # 20180421165643 #默认返回当前时间戳 if str: #如果传了时间的话 tp = time.strptime(str,format) #格式化好的时间,转成时间元组 res = time.mktime(tp)#再转成时间戳 else: res = time.time() #默认取当前的时间戳 return int(res)
datetime
import datetime print(datetime.datetime.today()) #获取当前时间,精确到秒 print(datetime.date.today()) #精确到天 res = datetime.datetime.today()+datetime.timedelta(days=1,minutes=5,seconds=5,weeks=5)几天后的,几天前的,几秒前的日期print(res.strftime('%Y-%m-%d'))
hashlib 加密模块
import hashlib # m = hashlib.md5() # # bytes # passwd = 'NHY_*&^_1982343532' # # passwd.encode() #把字符串转成bytes类型 # m.update(passwd.encode()) #不能直接对字符串加密,要先把字符串转成bytes类型 # print(m.hexdigest()) # #md5加密是不可逆 #撞库 # befor after # nhy123 81fb61ce98e508df8dbe8da07ad9acfc 关于加密只要调用my_md5方法即可 def my_md5(str): import hashlib new_str = str.encode() #把字符串转成bytes类型 # new_str = b'%s'%str #把字符串转成bytes类型 m = hashlib.md5() #实例化md5对象 m.update(new_str) #加密 return m.hexdigest() #获取结果返回
pymysql 操作数据库
import pymysql # 1、连上数据库 账号、密码 ip 端口号 数据库 #2、建立游标 #3、执行sql #4 、获取结果 # 5、关闭游标 #6、连接关闭 coon = pymysql.connect( host='***.***.**.**',user='xx',passwd='123456', port=3306,db='xx',charset='utf8' #port必须写int类型, #charset这里必须写utf8 ) cur = coon.cursor() #建立游标 # cur.execute('select * from stu;')#执行sql语句 cur.execute('insert into stu (id,name,sex) VALUE (1,"牛寒阳","女");') # delete update insert coon.commit() #必须得coomit res = cur.fetchall() #获取所有返回的结果 print(res) cur.close()#关闭游标 coon.close()#关闭连接
xlwt 操作excel表格
安装命令:pip install xlwt
import xlwt book = xlwt.Workbook() #新建一个excel sheet = book.add_sheet('sheet1')#加sheet页 sheet.write(0,0,'姓名')#行、列、写入的内容 sheet.write(0,1,'年龄') sheet.write(0,2,'性别') book.save('stu.xls')#结尾一定要用.xls
2.字典排序
d = {'a':8,'b':2,'c':3} #字典是无序,直接对字典排序是不存在的。 print(d.items()) res = sorted(d.items(),key=lambda x:x[1]) # sort,循环调用 print(res) for k,v in res: print(k,v)