如何使其余代码等待zipfile编译完文件

2024-06-26 14:53:57 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在编写一个脚本,它压缩多个文件,然后将zip文件上传到GoogleDrive。我设法分别执行这两个操作,但当我同时执行这两个操作时,上传会在文件压缩之前开始

# final part of the creating zip file process:
with zipfile.ZipFile(zipFileName+".zip", "w") as zf:
    for f in myFiles:
        zf.write(f)
        print(f)

# last part of the uploading process
file_name = (zipFileName+'.zip')
metadata = {'name': file_name,
            'mimeType': None
            }

res = DRIVE.files().create(body=metadata, media_body=file_name).execute()
if res:
    print('Uploaded "%s" (%s)' % (file_name, res['mimeType']))

我假设的解决方案是让zipfile进程等待,但我不确定如何进行。。我试图阅读zipfile文档,但找不到任何帮助


Tags: 文件ofthenamereszipprocessfile
1条回答
网友
1楼 · 发布于 2024-06-26 14:53:57

这基本上是对我有用的:

zipFileName = input("What do you want to call the zip file?: ")
try:
    zipTheFiles(zipFileName)
except:
    print("Failed to zip the files")
else:
    shipIt(zipFileName)

我将代码拆分为两个function,并将它们放在try、except、else语句中

相关问题 更多 >