(常用)time,datetime,random,shutil(zipfile,tarfile),sys模块
a、time模块
import time
import time
时间分为三种形式
1、时间戳 (时间秒数的表达形式, 从1970年开始)
print(time.time())
start_time=time.time()
time.sleep(3)
stop_time=time.time()
print(stop_time-start_time)
1、时间戳 (时间秒数的表达形式, 从1970年开始)
print(time.time())
start_time=time.time()
time.sleep(3)
stop_time=time.time()
print(stop_time-start_time)
2、格式化的字符串
print(time.strftime('%Y-%m-%d %H:%M:%S %p'))
print(time.strftime('%Y-%m-%d %X %p'))
print(time.strftime('%Y-%m-%d %H:%M:%S %p'))
print(time.strftime('%Y-%m-%d %X %p'))
3、struct_time对象(结构化时间)
print(time.localtime()) # 上海:东八区
print(time.localtime().tm_year)
print(time.localtime().tm_mday)
print(time.gmtime()) # UTC时区
print(time.localtime()) # 上海:东八区
print(time.localtime().tm_year)
print(time.localtime().tm_mday)
print(time.gmtime()) # UTC时区
4、之间转换
print(time.localtime(1111111111).tm_hour)
print(time.gmtime(1111111111).tm_hour)
print(time.mktime(time.localtime()))
print(time.strftime('%Y/%m/%d',time.localtime()))
print(time.strptime('2017/04/08','%Y/%m/%d'))
print(time.asctime(time.localtime()))
print(time.ctime(12312312321))
print(time.localtime(1111111111).tm_hour)
print(time.gmtime(1111111111).tm_hour)
print(time.mktime(time.localtime()))
print(time.strftime('%Y/%m/%d',time.localtime()))
print(time.strptime('2017/04/08','%Y/%m/%d'))
print(time.asctime(time.localtime()))
print(time.ctime(12312312321))
![](https://images2018.cnblogs.com/blog/1445809/201808/1445809-20180816182344475-2035210177.png)
![](https://images2018.cnblogs.com/blog/1445809/201808/1445809-20180816182351615-1876105513.png)
b、datetime模块
import datetime
import datetime
print(datetime.datetime.now())
print(datetime.datetime.now() + datetime.timedelta(days=3))
print(datetime.datetime.now() + datetime.timedelta(days=-3))
print(datetime.datetime.now() + datetime.timedelta(hours=3))
print(datetime.datetime.now() + datetime.timedelta(seconds=111))
current_time=datetime.datetime.now()
print(current_time.replace(year=1977))
print(datetime.date.fromtimestamp(1111111111))
print(datetime.date.fromtimestamp(time.time()) )
#时间戳直接转成日期格式 2018-04-08
print(datetime.datetime.now() + datetime.timedelta(days=3))
print(datetime.datetime.now() + datetime.timedelta(days=-3))
print(datetime.datetime.now() + datetime.timedelta(hours=3))
print(datetime.datetime.now() + datetime.timedelta(seconds=111))
current_time=datetime.datetime.now()
print(current_time.replace(year=1977))
print(datetime.date.fromtimestamp(1111111111))
print(datetime.date.fromtimestamp(time.time()) )
#时间戳直接转成日期格式 2018-04-08
c、random模块
import random
import random
print(random.random()) #0,1;大于0且小于1之间的小数
print(random.randint(1,3)) #大于等于1且小于等于3之间的整数
print(random.randrange(1,3)) #大于等于1且小于3之间的整数
print(random.choice([1,'a',[1,2,3]])) #从定义的列表中随机选取
print(random.sample([1,2,3,4,5],3)) #列表中元素任选(3)个数
print(random.uniform(1,3)) #大于1小于3的小数
item=[1,3,5,7,9]
random.shuffle(item) #打乱item的顺序相当于洗牌
print(item)
print(random.randint(1,3)) #大于等于1且小于等于3之间的整数
print(random.randrange(1,3)) #大于等于1且小于3之间的整数
print(random.choice([1,'a',[1,2,3]])) #从定义的列表中随机选取
print(random.sample([1,2,3,4,5],3)) #列表中元素任选(3)个数
print(random.uniform(1,3)) #大于1小于3的小数
item=[1,3,5,7,9]
random.shuffle(item) #打乱item的顺序相当于洗牌
print(item)
生成随机验证码
import random
import random
def make_code(n=5):
res=''
for i in range(n):
s1=str(random.randint(0,9))
s2=chr(random.randint(65,90))
res+=random.choice([s1,s2])
return res
print(make_code(10))
res=''
for i in range(n):
s1=str(random.randint(0,9))
s2=chr(random.randint(65,90))
res+=random.choice([s1,s2])
return res
print(make_code(10))
d、打印进度条
def progress(percent,width=50):
if percent > 1:
percent=1 #防止显示百分数超过100%
show_str=('[%%-%ds]' %width) %(int(width*percent) * '#')
print('\r%s %d%%' %(show_str,int(100*percent)),end='')
if percent > 1:
percent=1 #防止显示百分数超过100%
show_str=('[%%-%ds]' %width) %(int(width*percent) * '#')
print('\r%s %d%%' %(show_str,int(100*percent)),end='')
import time
recv_size=0
total_size=1111111
while recv_size < total_size:
time.sleep(0.1)
recv_size+=8096
percent=recv_size / total_size
progress(percent)
recv_size=0
total_size=1111111
while recv_size < total_size:
time.sleep(0.1)
recv_size+=8096
percent=recv_size / total_size
progress(percent)
#
print('[%-30s]'%'#') # %-50s, -左对齐, 50宽度为50不够空格补
print(('[%%-%ds]'%width)%'#') #第一个百分号取消了第二个百分号的特殊含义
print('[%-30s]'%'#') # %-50s, -左对齐, 50宽度为50不够空格补
print(('[%%-%ds]'%width)%'#') #第一个百分号取消了第二个百分号的特殊含义
num=30
print('%s%%' %num) #30%
print('%s%%' %num) #30%
e、shutil模块
压缩
import shutil
import time
ret = shutil.make_archive(
"day15_bak_%s" %time.strftime('%Y-%m-%d'),
'gztar',
root_dir=r'D:\code\SH_fullstack_s1\day15'
)
压缩
import shutil
import time
ret = shutil.make_archive(
"day15_bak_%s" %time.strftime('%Y-%m-%d'),
'gztar',
root_dir=r'D:\code\SH_fullstack_s1\day15'
)
f、(zipfile,tarfile) 模块
解压(r)
import tarfile
t=tarfile.open('day15_bak_2018-04-08.tar.gz','r')
t.extractall(r'D:\code\SH_fullstack_s1\day16\解包目录')
t.close()
import tarfile
t=tarfile.open('day15_bak_2018-04-08.tar.gz','r')
t.extractall(r'D:\code\SH_fullstack_s1\day16\解包目录')
t.close()
import zipfile
z=zipfileZipFile('解压文件','r')
z.extractall('解压路径')
z.close
压缩(w)
t=tarfile.open('压缩路劲','w')
t.add('压缩文件')
t.add('压缩文件')
t.close
z=zipfileZipFile('压缩路径','w')
z.write()
z.write()
z.close()
g、sys模块
sys.argv
sys.path
sys.argv
sys.path
模块内部的一般格式:
import 放在一起
import 放在一起
from import放在一起
全局变量的定义
功能函数的定义
if __name__==’__main__’:
func()
func()