To do this, the archive must be opened in read or append mode. You can also directly read the contents of a specific file from the archive using the read(file) method, where file is the name of the file that you intend to read. ![]() The order of these objects in the list is the same as that of actual zipfiles. In this case, you can use the infolist() method to return a list containing a ZipInfo object for every single member in the archive. We will come back to that later.Ĭalling the getinfo() method one by one on all files can be a tiresome process when there are a lot of files that need to be processed. This will give you access to information specific to that file, like the compressed and uncompressed size of the file or its last modification time. To get information on a specific file in the archive, you can use the getinfo() method of the ZipFile object. ![]() Using the namelist() method of the ZipFile object will return a list of all members of an archive by name. The zipfile module allows us to inquire about the contents of an archive without ever extracting it. Similarly, you may need to extract only those files which are larger than a specific size. Up to this point, your only option to do so is by extracting all the files in the archive. extract ( 'Fantasy Jungle.pdf', 'C: \\ Stories \\ Fantasy' )Ĭonsider a scenario where you need to see if a zip archive contains a specific file. ZipFile ( 'C: \\ Stories \\ Fantasy \\ archive.zip' ) as fantasy_zip :įantasy_zip.
0 Comments
Leave a Reply. |