有bug存在, 不知道是代码写的问题还是怎么回事 debug运行程序OK, 但是非debug启动, 程序是好是坏, 弃用
x 代表解压
a 代表压缩
import os
'''
zipSysDirString 7z.exe目录
pathZip 解压或者压缩文件
outPath 解压或者压缩的目录
zipFlg True代表解压 Fasle代表压缩
zipRemoveFlg True代表解压或者压缩完成后删除源文件 Fasle代表压缩
'''
def zipFile(zipSysDirString, pathZip, outPath, zipFlg, zipRemoveFlg):
sysStr = ''
if zipFlg:
sysStr = "\"" + zipSysDirString + "\"" + " x " + "\"" + pathZip + "\"" + " -o" + "\"" + outPath + "\""
else:
sysStr = "\"" + zipSysDirString + "\"" + " a " + "\"" + pathZip + "\"" + " -tzip -o" + "\"" + outPath + "\""
os.popen(sysStr)
if zipFlg and zipRemoveFlg:
os.remove(pathZip)
elif zipFlg == False and zipRemoveFlg:
recursiveDeletionFile(pathZip)
os.rmdir(pathZip)
def recursiveDeletionFile(dir_path):
# os.walk会得到dir_path下各个后代文件夹和其中的文件的三元组列表,顺序自内而外排列,
# 如 log下有111文件夹,111下有222文件夹:[('D:\\log\\111\\222', [], ['22.py']), ('D:\\log\\111', ['222'], ['11.py']), ('D:\\log', ['111'], ['00.py'])]
for root, dirs, files in os.walk(dir_path, topdown=False):
# 第一步:删除文件
for name in files:
os.remove(os.path.join(root, name)) # 删除文件
# 第二步:删除空文件夹
for name in dirs:
os.rmdir(os.path.join(root, name))
zipfile
进行解压缩文件解压缩文件名不要出现中文,容易出现错误
import os
import zipfile
# 出现乱码时解码
def recode(raw: str) -> str:
try:
return raw.encode('cp437').decode('shift-jis')
except:
return raw.encode('utf-8').decode('utf-8')
'''
pathZip 压缩包路径
outPath 解压后输出目录位置
zipRemoveFlg 解压后是否删除源文件
'''
def zipFile(pathZip, outPath, zipRemoveFlg):
zipFile = zipfile.ZipFile(pathZip) # 压缩包路径
zipFileList = zipFile.namelist() # 获取压缩包里所有文件
for f in zipFileList:
zipFile.extract(f, outPath) # 循环解压文件到指定目录
name1 = os.path.join(outPath, f) # 乱码文件名
name2 = os.path.join(outPath, recode(f)) # 解码后文件名
os.rename(name1, name2) # 文件重命名
zipFile.close() # 关闭文件释放内存
if zipRemoveFlg:
os.remove(pathZip)
import os
import zipfile
'''
dirpath 需要压缩文件目录
outFullName 压缩后输出目录位置
zipRemoveFlg 解压后是否删除源文件
'''
def zipDir(dirpath, outFullName, zipRemoveFlg):
zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED)
for path, dirnames, filenames in os.walk(dirpath):
# 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
fpath = path.replace(dirpath, '')
for filename in filenames:
zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
zip.close()
if zipRemoveFlg:
recursiveDeletionFile(dirpath)
os.rmdir(dirpath)