Python standard-library module provides tools to create, read, write, append, and list a ZIP file.
Questions tagged [python-zipfile]
404 questions
6
votes
1 answer
load a pickle file from a zipfile
For some reason I cannot get cPickle.load to work on the file-type object returned by ZipFile.open().
If I call read() on the file-type object returned by ZipFile.open() I can use cPickle.loads though.
Example ....
import zipfile
import cPickle
#…

eric.frederich
- 1,598
- 4
- 17
- 30
6
votes
1 answer
Unable to Create ZIPfile using python
I am trying to create zipfile using python 3.4.
Upon execution following error is shown:
AttributeError: 'module' object has no attribute 'Zipfile'
My code:
import zipfile
f= zipfile.Zipfile("testZIP.zip","w")

user3832232
- 61
- 1
- 2
6
votes
1 answer
Python zipfile module creates multiple files with same name
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 creates an archive with two files…

Kshitiz Sharma
- 17,947
- 26
- 98
- 169
6
votes
1 answer
How to get the filepath of a ZipFile object?
If I have a zipfile.ZipFile object, how can I determine the file path of the zip file from the object?
a = zipfile.ZipFile('C:\\path\\zipfile.zip')
a.get_file_path()
where get_file_path() should return 'C:\\path\\zipfile.zip'.

kiri
- 2,522
- 4
- 26
- 44
6
votes
1 answer
Python Zipfile - Invalid Argument Errno 22
I have a .zip file and would like to know the names of the files within it. Here's the code:
zip_path = glob.glob(path + '/*.zip')[0]
file = open(zip_path, 'r') # opens without error
if zipfile.is_zipfile(file):
print str(file) # prints to…

pcaisse
- 754
- 2
- 11
- 19
5
votes
1 answer
Unable to unzip a .zip file with a password with python zipfile library
I created a zip file with Gnome Archive Manager (Ubuntu OS). I created the zip file with a password and I am trying to unzip it using the zipfile Python library:
import zipfile
file_name = '/home/mahmoud/Desktop/tester.zip'
pswd = 'pass'
with…

Mahmoud Abdel-Rahman
- 497
- 2
- 10
- 27
5
votes
4 answers
Can't unzip archive built with zipfile (Python)
I'm having problems with an archive that I built using zipfile in Python. I'm iterating over all the files in a directory and writing them to an archive. When I attempt to extract them afterward I get an exception related to the path…

Cat
- 7,042
- 8
- 34
- 36
5
votes
1 answer
save multiple objects to zip directly from memory
I'd like to save multiple objects, e.g. figures, created in a loop directly to a zip file, without saving them in directory.
At the moment I'm saving the figures in a folder and then zipping them.
import matplotlib.pyplot as plt
from zipfile…

Alessandro Bitetto
- 101
- 1
- 8
5
votes
1 answer
Python zipfile dosen't release zip file
I'm trying to use zipfile library on windows 8.1 and python 2.7.9.
I just want to remove library.zip after zipfile.open() but os.remove() throws "WindowsError [Error 32]" and it seems zipfile doesn't release the zip file out of with…

Nesswit
- 53
- 1
- 4
4
votes
2 answers
Is reading from Python zipfile thread-safe?
I have seen several differing opinions on this.
I don't see anything in the latest docs (3.9.2).
Can I read multiple different entries in a ZipFile safely?
I have seen some unusual errors like "Error -3 while decompressing data: invalid stored block…

Paul Draper
- 78,542
- 46
- 206
- 285
4
votes
1 answer
Unzipping a .docx file using zipfile library
I am trying write an application gets information from a table in a word docx file in order to do some analysis on it by putting turning it into a pandas DataFrame. The first step is properly reading and the docx file, and to do this, I am following…

Maximus3537
- 45
- 2
- 7
4
votes
1 answer
Get root folder of extracted Zipfile
Using Zipfile in Python 3.6, I am able to successfully extract a zip using:
with zipfile.ZipFile(my_zip,"r") as zip_ref:
zip_ref.extractall('./download')
The zip will always contain one top level folder:
with a unique ID filename that is…

jakc
- 1,161
- 3
- 15
- 42
4
votes
1 answer
Using zipfile to archive directory contents while skipping files from list
I'm using zipfile to create an archive of all files in a directory (recursively, while preserving directory structure including empty folders) and want the process to skip the filenames specified in a list.
This is the basic function that os.walks…

noob
- 328
- 2
- 13
4
votes
1 answer
Could not find a version that satisfies the requirement zipfile (from versions: )
I used pip install zipfile, and error went out:
Could not find a version that satisfies the requirement zipfile (from
versions: ) No matching distribution found for zipfile
Addition Info: pip 18.1 from
…

Boat.Xin
- 51
- 1
- 4
4
votes
1 answer
Python ZipFile return extracted file path and name
I have this current code to unzip the contents of archive to extract_dir. However, I cannot figure out how to get the extracted file path & name of the extracted file.
if archive.endswith((".zip")):
zip_ref = zipfile.ZipFile(archive, 'r')
…

user7399815
- 323
- 1
- 3
- 10