I have following code in python:
>>>
import zipfile
>>>
zip = zipfile.ZipFile('abc.zip', 'w') >>>
zip.writestr('myfile', 'This is sample text') >>>
zip.writestr('myfile', 'This is sample text') >>>
zip.close()
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide., Help Tracker Documentation Tracker Development Report Tracker Problem
ZipFile allows to add the same file to archive twice.I bet it is not
intended behavior
for many users who would like to either replace file
inside of archive or get runtime warning about duplicate file to be
added.http: //code.google.com/p/gvn/issues/detail?id=63
from zipfile
import ZipFile
zt = ZipFile("ziptest.zip", "w")
zt.write("ziptest.py")
zt.write("ziptest.py")
zt.close()
I think a warning would be sensible here.The behavior is certainly not
what I would expect.
How about adding optional "replace=True"
attribute to the write method ?
So that people who are not aware enough to adjust the code and handle
new warning could still get valid archives.
The mechanism
for throwing an error has been written, however
for the
case of a duplicated filename, it appears to have been deliberatly not
been used by the original author.:
def _writecheck(self, zinfo):
""
"Check for errors before writing a file to the archive."
""
if zinfo.filename in self.NameToInfo:
if self.debug: # Warning
for duplicate names
print "Duplicate name:", zinfo.filename
if self.mode not in ("w", "a"):
raise RuntimeError, 'write() requires mode "w" or "a"'
...
Putting a 'replace=True'
switch seems a little clumsy, it would be much
better to raise an error, or allow the user a way to globally control
what happens in this
case, i.e.overwrite the existing file or drop it.
Adding a global behaviour
switch seems to be the best way to preserve
backwards compatibility.
What do people think is the best way ?
--Martin
a similar problem appears when the zip file contains two files as in: ['_test/tree.pl', '_test/'].
for ZipFile.extractall() when _test / tree.pl is extracted _test / is
created and the the extraction of _test / fails because OSError: [Errno
17
] File exists: '_test/'
Still an issue for Python 2.7
The ZIP file format is a common archive and compression standard. This module provides tools to create, read, write, append, and list a ZIP file. Any advanced use of this module will require an understanding of the format, as defined in PKZIP Application Note.,If the file is created with mode 'w', 'x' or 'a' and then closed without adding any files to the archive, the appropriate ZIP structures for an empty archive will be written to the file.,The zipfile module provides a simple command-line interface to interact with ZIP archives.,Return a list containing a ZipInfo object for each member of the archive. The objects are in the same order as their entries in the actual ZIP file on disk if an existing archive was opened.
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')
with ZipFile('spam.zip') as myzip:
with myzip.open('eggs.txt') as myfile:
print(myfile.read())
>>> Path(...).joinpath('child').joinpath('grandchild') >>>
Path(...).joinpath('child', 'grandchild') >>>
Path(...) / 'child' / 'grandchild'
>>> zf = PyZipFile('myprog.zip') >>>
def notests(s):
...fn = os.path.basename(s)
...
return (not(fn == 'test'
or fn.startswith('test_'))) >>>
zf.writepy('myprog', filterfunc = notests)
string.pyc # Top level name
test / __init__.pyc # Package directory
test / testall.pyc # Module test.testall
test / bogus / __init__.pyc # Subpackage directory
test / bogus / myfile.pyc # Submodule test.bogus.myfile
$ python - m zipfile - c monty.zip spam.txt eggs.txt
In this article we will discuss how to create a zip archive from selected files or files from a directory based on filters.,Python’s zipfile module provides a ZipFile class for zip file related stuff. Let’s use this to create a zip archive file.,To zip selected files from a directory we need to check the condition on each file path while iteration before adding it to zip file.,this code is not working as per the expection…in this deirectory there are 2 files i want to keep those files as a sampleDir.zip zip file. some could please help what i did wrong here
First import the class from module i.e.
from zipfile
import ZipFile
# create a ZipFile object zipObj = ZipFile('sample.zip', 'w') # Add multiple files to the zip zipObj.write('sample_file.csv') zipObj.write('test_1.log') zipObj.write('test_2.log') # close the Zip File zipObj.close()
# Create a ZipFile Object with ZipFile('sample2.zip', 'w') as zipObj2: # Add multiple files to the zip zipObj2.write('sample_file.csv') zipObj2.write('test_1.log') zipObj2.write('test_2.log')
Let’s create function that Iterates over a directory and filter the contents with given callback. Files which pass the filter will only be added in zip i.e.
from zipfile
import ZipFile
import os
from os.path
import basename
# Zip the files from given directory that matches the filter
def zipFilesInDir(dirName, zipFileName, filter):
# create a ZipFile object
with ZipFile(zipFileName, 'w') as zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(dirName):
for filename in filenames:
if filter(filename):
# create complete filepath of file in directory
filePath = os.path.join(folderName, filename)
# Add file to zip
zipObj.write(filePath, basename(filePath))
Complete example is as follows:
from zipfile
import ZipFile
import os
from os.path
import basename
# Zip the files from given directory that matches the filter
def zipFilesInDir(dirName, zipFileName, filter):
# create a ZipFile object
with ZipFile(zipFileName, 'w') as zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(dirName):
for filename in filenames:
if filter(filename):
# create complete filepath of file in directory
filePath = os.path.join(folderName, filename)
# Add file to zip
zipObj.write(filePath, basename(filePath))
def main():
print('*** Create a zip file from multiple files ')
#create a ZipFile object
zipObj = ZipFile('sample.zip', 'w')
# Add multiple files to the zip
zipObj.write('sample_file.csv')
zipObj.write('test_1.log')
zipObj.write('test_2.log')
# close the Zip File
zipObj.close()
print('*** Create a zip file from multiple files using with ')
# Create a ZipFile Object
with ZipFile('sample2.zip', 'w') as zipObj2:
# Add multiple files to the zip
zipObj2.write('sample_file.csv')
zipObj2.write('test_1.log')
zipObj2.write('test_2.log')
# Name of the Directory to be zipped
dirName = 'sampleDir'
# create a ZipFile object
with ZipFile('sampleDir.zip', 'w') as zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(dirName):
for filename in filenames:
#create complete filepath of file in directory
filePath = os.path.join(folderName, filename)
# Add file to zip
zipObj.write(filePath)
print('*** Create a zip archive of only csv files form a directory ***')
zipFilesInDir('sampleDir', 'sampleDir2.zip', lambda name: 'csv' in name)
if __name__ == '__main__':
main()
Last Updated : 22 Jul, 2021,GATE CS 2021 Syllabus
from zipfile
import ZipFile
with ZipFile(file_name, 'r') as zip:
zip.printdir()
zip.extractall()
extractall() method will extract all the contents of the zip file to the current working directory. You can also call extract() method to extract any file by specifying its path in the zip file.
For example:
zip.extract('python_files/python_wiki.txt')
If you want to read some specific file, you can go like this:
data = zip.read(name_of_file_to_read)
def get_all_file_paths(directory):
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
file_paths.append(filepath)
return file_paths