format函数格式化输出
: 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充。
^, <, > 分别是居中、左对齐、右对齐,后面带宽度
+ 表示在正数前显示 +,负数前显示 -; (空格)表示在正数前加空格
b、d、o、x 分别是二进制、十进制、八进制、十六进制。
print('{:0>10d}'.format(1))
print('{:+.2f}'.format(-1.23))
模拟进度条
/r:每次在行首打印
import time
res = ''
for i in range(50):
res+='#'
time.sleep(0.3)
print('\r[%-50s]'%res,end='')
场景应用
模拟数据下载
import time
recv_size = 0
total_size = 333333
def progress(percent):
if percent > 1 :
percent = 1
res = int(50*percent) * '#'
print('\r[{:<50}] {}%'.format(res,int(100*percent)),end='')
while recv_size<total_size:
time.sleep(0.3)
recv_size+=5000
percent = recv_size / total_size
progress(percent)
shutil模块(文件操作)
shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))
shutil.copyfile('f1.log', 'f2.log')
shutil.copy('f1.log', 'f2.log')
shutil.copy2('f1.log', 'f2.log')
shutil.copymode('f1.log', 'f2.log')
shutil.copystat('f1.log', 'f2.log')
shutil.ignore_patterns(*patterns)
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
shutil.rmtree('folder1')
shutil.move('folder1', 'folder3')
ret = shutil.make_archive("ww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')
import tarfile
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.log', arcname='bbs2.log')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.log', arcname='cmdb.log')
tar.close()
tar = tarfile.open('your.tar','r')
tar.extractall()
tar.close()