Python standard-library module provides tools to create, read, write, append, and list a ZIP file.
Questions tagged [python-zipfile]
404 questions
10
votes
3 answers
zipfile cant handle some type of zip data?
I came up over this problem while trying to decompress a zip file.
-- zipfile.is_zipfile(my_file) always returns False, even though the UNIX command unzip handles it just fine. Also, when trying to do zipfile.ZipFile(path/file_handle_to_path) I get…

hyperboreean
- 8,273
- 12
- 61
- 97
10
votes
1 answer
Using openpyxl module to write to spreadsheet creates a damaged spreadsheet, how to fix with zipfile module?
I have a program which writes to a spreadsheet using openpyxl. Upon execution of the program, the cells are filled as expected but the spreadsheet becomes damaged. Excel repairs the spreadsheet and I can then view it again.
import openpyxl
from…

August Williams
- 907
- 4
- 20
- 37
10
votes
1 answer
How to send a dynamically generate zipfile to the client
I am looking for a way to send a zipfile to the client that is generated from a requests response. In this example, I send a JSON string to a URL which returns a zip file of the converted JSON string.
@app.route('/sendZip', methods=['POST'])
def…

camdenl
- 1,159
- 4
- 21
- 32
9
votes
1 answer
ZIp only contents of directory, exclude parent
I'm trying to zip the contents of a directory, without zipping the directory itself, however I can't find an obvious way to do this, and I'm extremely new to python so it's basically german to me.
here's the code I'm using which successfully…

Flux
- 93
- 1
- 4
9
votes
2 answers
seek() a file within a zip file in Python without passing it to memory
is there anyway to make a file inside a zip file seekable in Python without reading it to memory?
I tried the obvious procedure but I get an error since the file is not seekable:
In [74]: inputZipFile =…

jbssm
- 6,861
- 13
- 54
- 81
9
votes
4 answers
Zipping files in python
My program runs smoothly but I want my files from ftp to be zip in my local drive
The problem is only 1 file is being zipped after calling my main() function
Here's my code:
import os
import upload
import download
import zipfile
import…

neo
- 293
- 3
- 7
- 21
8
votes
5 answers
Extract files from zip without keep the top-level folder with python zipfile
I'm using the current code to extract the files from a zip file while keeping the directory structure:
zip_file = zipfile.ZipFile('archive.zip', 'r')
zip_file.extractall('/dir/to/extract/files/')
zip_file.close()
Here is a structure for an example…

xsquirrel
- 139
- 2
- 10
8
votes
2 answers
Exclude a directory from getting zipped using zipfile module in python
I am trying to zip a directory using python zipfile module and its working well.But now i want to exclude some folders.ie if my director tree is like
abc
def
ghi
jkl
mno
then i want to archive all to myfile.zip but excluding "ghi"
I am trying to…

thinkingmonster
- 5,063
- 8
- 35
- 57
7
votes
1 answer
Python ZipFile path separators
If I am unzipping a Zip file with the Python ZipFile library, where the file was created on Windows, but my code is running on Unix-like, will the path separators always be Unix-style?

Joe
- 46,419
- 33
- 155
- 245
7
votes
1 answer
zipfile: how to set a password for a Zipfile?
I have a zipfile in (sav.zip) and I'm trying to set a password for it:
zf = zipfile.ZipFile("sav.zip")
zf.setpassword("1234")
but I get a TypeError: expected Bytes, got str.
Where's my mistake?
user7875282
7
votes
1 answer
Preserve file attributes in ZipFile
I'm looking for a way to preserve the file attributes (eg. read-only) of a file that gets written to a zipfile.ZipFile instance.
The files I add to the zip archive gets their file attributes reset, eg. the read-only flag is gone when inspecting the…

aelgn
- 821
- 1
- 11
- 17
7
votes
2 answers
Python zipfile module doesn't compress files
I have a problem with compression in Python.
I know I should call the ZIP_DEFLATED method when writing to make the zip file compressed, but it does not work for me.
I have 3 PDF documents in the C:zip directory.
When I run the following code it…

user2665140
- 126
- 3
- 10
6
votes
1 answer
How to know the folder size in a zipfile
I know that you can get the size in bytes of a file in a ZIP file using the .file_size method
But is there any what I can get the size of a folder instead?
Ex:
import zipfile, os
os.chdir('C:\\')
zp= zipfile.ZipFile('example.zip')
spamInfo =…

tadm123
- 8,294
- 7
- 28
- 44
6
votes
1 answer
Write text into zipfile in python
I have a function which creates a zip archive with a textfile in it.
Is it possible to do this without creating a temporary file?
import zipfile
import os
PROJ_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)))
def create_zip(name,…

Anton
- 2,217
- 3
- 21
- 34
6
votes
2 answers
How can I avoid: "ZipFile instance has no attribute '__exit__''" when extracting a zip file?
The code is:
import sys
execfile('test.py')
In test.py I have:
import zipfile
with zipfile.ZipFile('test.jar', 'r') as z:
z.extractall("C:\testfolder")
This code produces:
AttributeError ( ZipFile instance has no attribute '__exit__' ) #…

george
- 685
- 2
- 9
- 22