【发布时间】:2020-09-04 02:42:26
【问题描述】:
我的代码有问题,请帮助我更正我的代码
import glob
import os
import shutil from zipfile
import ZipFile from os
import path from shutil
import make_archive
def main():
list_of_files = glob.glob('C:\\Users\\RezaPal\\Desktop\\test compress\\.docx')
latest_file = max(list_of_files, key=os.path.getctime)
# Check if file exists
if path.exists(latest_file):
# get the path to the file in the current directory
src = path.realpath(latest_file);
# rename the original file
#os.rename("backup_filetest.docx","filetest.docx")
# now put things into a ZIP archive
#root_dir,tail = path.split(src)
#shutil.make_archive("guru99 archive", "zip", root_dir)
# more fine-grained control over ZIP files
with ZipFile("backup_filetest.zip","w") as newzip:
newzip.write("filetest.docx")
newzip.write("filetest.docx.bak")
if __name__== "__main__":
main()
【问题讨论】:
-
缩进在 Python 中非常重要。你的不正确。 docs.python.org/2.0/ref/indentation.html
-
您需要正确格式化代码。块应该缩进 4 个空格或制表符(不要同时使用)
标签: python zip compression