简体   繁体   中英

How to ensure that all files were successfully zipped while using the zip() in python,is there any function to check or acknowledge?

我使用shutil .make_archive(“arch”,“zip”,path)压缩所有文件,并想知道是否所有文件都是压缩的。我使用下面的代码来压缩文件。

shutil.make_archive("arch", "zip", r"path")

as pointed out by Kevin/others, in Python (and as most languages with "exception handling") you can expect that most routines will raise/throw an exception on error conditions

if you care about the details, the relevant Python source is in shutil and zipfile where you can see that they use appropriate "context managers" and raise exceptions as appropriate, but mostly rely on lower level file operations to throw

if you want some human output to monitor progress, you can use the logging infrastructure, eg:

import logging
logging.basicConfig(level=0)

shutil.make_archive("arch", "zip", "path", logger=logging)

(see the tutorial for more details) will cause it to give more output

I don't understand why would you want to do that ? If make_archive did not raises any exceptions, all files in path are supposed to be in arch .

If you want to know it anyway, you can either :

  • unzip the archive with shutil.unpack_archive

  • use zipfile to read the archive like this :

    from ZipFile import ZipFile

    with ZipFile("arch.zip") as myzip:
    print(myzip.namelist())

Example 3 at this link seems to indicate that shutil.make_archive returns a string with the path and file name of the file that was created. It shows that string being used to check the newly created archive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM