常用模块
时间模块 import time
主要是用来和时间打交道
时间格式
格式化时间 字符串数据类型 给人看的
1 str_time = time.strptime('2008-8-8', '%Y-%m-%d') 2 print(str_time) 3 res = time.mktime(str_time) 4 print(res)
结构化时间 time.localtime()
1 s_time = time.localtime(3000000000) 2 res = time.strftime('%Y-%m-%d %H:%M:%S', s_time) 3 print(res)
时间戳时间 浮点型数据类型 以秒为单位 主要是给计算机用的 时间戳以1970-1-1 0:00:00 开始计时
print(time.time())
时间戳换成字符串时间
1 s_time = time.localtime(3000000000) 2 res = time.strftime('%Y-%m-%d %H:%M:%S', s_time) 3 print(res)
字符串转时间戳
struct_time = time.strptime('1984-10-07 5:30:00', '%Y-%m-%d %H:%M:%S') res = time.mktime(struct_time) print(res)
计算时间差
1 # 2018-8-19 22:10:9 2018-8-20 17:33:45 2 3 str_time1 = '2018-8-19 22:10:9' 4 str_time2 = '2018-8-20 17:33:45' 5 st_time1 = time.strptime(str_time1, '%Y-%m-%d %H:%M:%S') 6 st_time2 = time.strptime(str_time2, '%Y-%m-%d %H:%M:%S') 7 res1 = time.mktime(st_time1) 8 res2 = time.mktime(st_time2) 9 sub = res2-res1 10 gm_time = time.gmtime(sub) 11 print('过去了%d年%d月%d日%d小时%d分%d秒' % (gm_time.tm_year-1970, gm_time.tm_mon-1, gm_time.tm_mday-1, 12 gm_time.tm_hour, gm_time.tm_min, gm_time.tm_sec))
random 取随机数的模块
import random
随机取小数:数学计算
1 print(random.random()) # 取0-1之间的小数 2 print(random.uniform(1,2)) # 取1-2之间的小数
取随机整数: 彩票 抽奖
1 print(random.randint(1,2)) # [1,2] 2 print(random.randrange(1,2)) # [1,2) 3 print(random.randrange(1,200,2)) # [1,2)
验证码
4位数字验证码、6位数字验证码、6位数字+字母验证码
4位数字验证码:
1 s = '' 2 for i in range(4): 3 num = random.randint(0,9) 4 s += str(num) 5 print(s)
6位数字验证码:
1 s = '' 2 for i in range(6): 3 num = random.randint(0,9) 4 s += str(num) 5 print(s)
def code(n=6):
s = ''
for i in range(n):
num = random.randint(0,9)
s += str(num)
return s
print(code(4))
print(code())
6位数字+字母验证码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 def code(n = 6,alpha = True): 2 s = '' 3 for i in range(n): 4 # 生成随机的大写字母,小写字母,数字各一个 5 if alpha: 6 num = str(random.randint(0,9)) 7 alpha_upper = chr(random.randint(65,90)) 8 alpha_lower = chr(random.randint(97,122)) 9 res = random.choice([num,alpha_upper,alpha_lower]) 10 s += res 11 else: 12 num = random.randint(0, 9) 13 s += str(num) 14 return s 15 print(code(4)) 16 print(code()) 17 18 def code(n = 6,alpha = True): 19 s = '' 20 for i in range(n): 21 num = str(random.randint(0,9)) 22 if alpha: 23 alpha_upper = chr(random.randint(65,90)) 24 alpha_lower = chr(random.randint(97,122)) 25 num = random.choice([num,alpha_upper,alpha_lower]) 26 s += num 27 return s 28 print(code(4,False)) 29 print(code(alpha=False))
s模块
os是和操作系统交互的模块 import os
文件和文件夹相关
os.makedirs 创建多级目录 os.mkdir 创建单个文件夹 os.removedirs 删除多级 删除空文件夹 os.rmdir 删除多个删除空文件夹 os.listdir() 查看当前目录下的所有文件和文件夹 os.stat 获得文件的信息 os.path.getatime os.path.getmtime os.path.getsize 查看文件大小
文件路径相关
os.path.abspath() 把路径中不符合规范的/改成操作系统的\ 能找到相对路径改成绝对路径 os.path.split() 把一个路径分成两段,第二段是一个文件或者文件夹 os.path.dirname 获取到绝对路径的文件夹名字 os.path.basename 获取到文件的名字 os.path.join 拼接路径 os.path.exists 判断文件或者文件夹是否存在 os.path.isabs 判断文件或者路径是否是绝对路径 os.path.isfile 判断是否是文件 os.path.isdir 判断是否是文件夹
执行操作系统
os.system('dir') 执行操作系统的命令,没有返回值,实际造作/删除一个文件夹 创建一个文件夹 os.popen('dir') 做和查看类的操作系统的命令,并返回结果 os.getcwd() 在当前工作目录 并不是当前文件所在目录 当前文件在哪个目录下执行的 os.chdir() 修改当前执行命令的时候所在的目录
sys模块
sys.argv 当你在命令行执行python文件,而不是在pycharm中执行这个文件的时候 你的命令>>>python python文件的路径 参数1 参数2 参数3 ...... sys.argv = ['python文件的路径',参数1 参数2 参数3 ......] 好处 :这些需要输入的参数不需要在程序中以input的形式输入了 argv的第一个参数 是python这个命令后面的值
序列化模块
impor json
dump/load 两个在文件中执行
dumps/loads 字符串/字节 < ------->其他数据
不支持多次dump和load
import pickle
只能在python中使用
支持在python中所有数据类型
在和文件操作的时候,需要用wb rb的模式打开文件
可以多次dump和多次load