Python zipfile module

Python zipfile module can read and create ZIP format compressed file.

is_zipfile() method checks whether the file is ZIP compressed file or not:

>>> import zipfile
>>> zipfile.is_zipfile('help.zip')
True
namelist() method lists all file names in the ZIP file:
>>> import zipfile
>>> z = zipfile.ZipFile('help.zip','r')
>>> print(z.namelist())
['help/dataset.txt', 'help/dely.php', 'help/output.txt']
infolist() method lists more information of the Zip file:
>>> for f in z.infolist():
... print(f.filename, " ", f.file_size, " ", f.compress_size)
...
help/dataset.txt 339131 55811
help/dely.php 1689 575
help/output.txt 55708 15366
read() method reads the content of the compressed files:
>>> for f in z.infolist():
... str = z.read(f.filename)
... print (repr(str))
...

write() method writes into the ZIP file:
>>> import zipfile
>>> z = zipfile.ZipFile('help2.zip','w')
>>> z.write('output.txt')
>>> z.write('dataset.txt')
>>> z.write('dely.php')













endmemo.com © 2024  | Terms of Use | Privacy | Home