shutil模块与datetime模块 & 文件的解压及压缩

-----------------------------shutil模块与datetime模块--------------------------

shutil模块

import shutil
#删除一个目录,没有返回值
shutil.rmtree('x.txt')
shutil.move('bblun','lbin')
#更改同一级文件或文件夹名称
shutil.make_archive('zzh','zip',r'C:HGin') 
#将该路径下的文件压缩成后缀为zip名称为zzh的压缩包
shutil.unpack_archive('zzh.zip',extract_dir='C:HG',format='zip')

#或者shutil.unpack_archive('zzh.zip','C:HG','zip'),同级目录可不写文件的路径,直接写文件的名字就行
#解压文件,从左至右依次为,压缩文件名,解压到的目录

datetime模块补充

from datetime import datetime
ctime = datetime.now()
print(ctime,type(ctime))#打印当前时间,datetime类型
ctime = datetime.now().strftime('%Y-%m-%d-%H-%M-%S') #按照年月日时分秒分单位打印
print(ctime,type(ctime))#打印当前时间,字符串类型

同级目录下文件的路径的拼接,压缩与解压

ctime= 'yangzilie'
import os
import shutil
if not os.path.exists('chang'):
    os.makedirs('chang')
shutil.make_archive(os.path.join('cheng',ctime),'zip','C:HGlibcoaa')
#在文件夹cheng后进行拼接
file_path = os.path.join('cheng',ctime)+'.zip'#路径与文件的拼接
shutil.unpack_archive(file_path,r'D:code','zip')

解压任意一个解压文件

import shutil
file = r'D:CNTVlli.zip'
shutil.unpack_archive(file,r'D:','zip')
原文地址:https://www.cnblogs.com/yangzilaing/p/13383381.html