6

I have been trying to make a python script to zip a file with the zipfile module. Although the text file is made into a zip file, It doesn't seem to be compressing it; testtext.txt is 1024KB whilst testtext.zip (The code's creation) is also equal to 1024KB. However, if I compress testtext.txt manually in File Explorer, the resulting zip file is compressed (To 2KB, specifically). How, if possible, can I combat this logical error?

Below is the script that I have used to (unsuccessfully) zip a text file.

from zipfile import ZipFile

textFile = ZipFile("compressedtextstuff.zip", "w")
textFile.write("testtext.txt")
textFile.close()

3 Answers 3

13

Well that's odd. Python's zipfile defaults to the stored compression method, which does not compress! (Why would they do that?)

You need to specify a compression method. Use ZIP_DEFLATED, which is the most widely supported.

import zipfile
zip = zipfile.ZipFile("stuff.zip", "w", zipfile.ZIP_DEFLATED)
zip.write("test.txt")
zip.close()
Sign up to request clarification or add additional context in comments.

2 Comments

I tested that code, it worked fine, and then I copied and pasted it into the answer. You would need to post your code for us to see what you did wrong.
You are right. I deleted it because the answer to my suggestion is already answered in SO
6

From the https://docs.python.org/3/library/zipfile.html#zipfile-objects it suggest example:

with ZipFile('spam.zip', 'w') as myzip:
    myzip.write('eggs.txt')

So your code will be

from zipfile import ZipFile
 
with ZipFile('compressedtextstuff.zip', 'w', zipfile.ZIP_DEFLATED) as myzip:
    myzip.write('testtext.txt')

Comments

-1

https://docs.python.org/3/library/zipfile.html#:~:text=with%20ZipFile(%27spam.zip%27%2C%20%27w%27)%20as%20myzip%3A%0A%20%20%20%20myzip.write(%27eggs.txt%27)

In the docs they have it written with a with statement so I would try that first.

Edit:

I just came back to say that you have to specify your compression method but Mark beat me to the punch.

Here is a link to a StackOverflow post about it https://stackoverflow.com/questions/4166447/python-zipfile-module-doesnt-seem-to-be-compressing-my-files#:~:text=This%20is%20because%20ZipFile%20requires,the%20method%20to%20be%20zipfile.

4 Comments

Unfortunately, when using the code provided, the outputted zip file is in fact 1025KB, which is larger than the original text file. Which is weird.
is the text file randomly generated?
kinda, the text file is just 1,000,000 zeros (for test purposes)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.