【问题标题】:expected an indented block Python script compress预期缩进块 Python 脚本压缩
【发布时间】: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 zip compression


【解决方案1】:

正确代码:

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()

我不知道应该如何执行“whith”块:是否有条件。考虑这一点。

【讨论】:

    猜你喜欢
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多