import zipfile
target = 'OLD-BACKUP.zip'
handle = zipfile.ZipFile(target)
listing = handle.namelist()
for x in listing:
res = handle.getinfo(x)
print('-' * 79)
print('Filename: ', res.filename)
print('Before: ', res.file_size)
print('After: ', res.compress_size)
This code imports the zipfile
module and then opens a ZIP file named 'OLD-BACKUP.zip'
using the ZipFile()
function from the zipfile
module.
The code then uses the namelist()
method to get a list of all the files and directories present in the ZIP archive and stores the list in a variable named listing
.
The code then loops through the listing
variable and for each item in the list:
- It gets information about the item in the ZIP archive using the
getinfo()
method of theZipFile
objecthandle
and stores the information in a variable namedres
. - It then prints a separator of 79 dashes to visually separate the output for each item in the archive.
- It prints the filename of the item in the archive, obtained from the
filename
attribute of theZipInfo
objectres
. - It prints the size of the file before compression, obtained from the
file_size
attribute of theZipInfo
objectres
. - It prints the size of the file after compression, obtained from the
compress_size
attribute of theZipInfo
objectres
.
So, this code essentially displays the filenames, original sizes, and compressed sizes of all the files and directories present in the 'OLD-BACKUP.zip'
archive.
No comments:
Post a Comment