【发布时间】:2018-06-05 13:24:45
【问题描述】:
我正在使用此功能(基于this awnser)在子进程中使用 7zip 将文件夹压缩为加密的 .zip 文件:
def zipbkp(directoryToZip):
zipFileName = directoryToZip+".zip"
password = "minmin3"
appPath = "C:\Program Files\\7-Zip"
zApp = "7z.exe"
zAction = 'a'
zPass = '-p{0} -mhe'.format(password)
zAnswer = '-y'
zDir = directoryToZip
progDir = os.path.join(appPath,zApp)
print("[*] Trying to create .zip File, please wait...")
cmd = [zApp, zAction, zipFileName, zPass, zAnswer, zDir]
zipper = subprocess.Popen(cmd, executable=progDir, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
#Lacking Progressbar here
zipper.wait()
print("[*] Successfully created .zip File")
return zipFileName
这可以正常工作。 我唯一的问题是由于要压缩的目录非常大,我想提供有关压缩进度的信息。
我之前已成功安装并使用tqdm 来显示进度条,但无法让它与这个 7zip 子进程一起使用。
作为参考,这是我在 ftp 上传脚本上使用 tqdm 的方式:
with tqdm(unit = 'blocks', unit_scale = True, leave = False, miniters = 1, desc = 'Uploading', total = filesize) as tqdm_instance:
ftp.storbinary('STOR ' + filename, tmp, 2048, callback = lambda sent: tqdm_instance.update(len(sent)))
tqdm 甚至提供了如何使用管道的示例,但我不太明白 它。该示例还使用了 Windows 上不可用的 grep。
我还发现了this example,这更难理解。
知道如何使用 tqdm 获取和解析 7zip 提供的信息吗?
【问题讨论】:
标签: python python-3.x progress-bar 7zip tqdm