codey codey - 1 year ago 108
Python Question

Python compressing a simple text file

I am a beginner in python. I'm not to sure how to compress a simple text file with the user's sentence. Is the easiest way to go about it by using ".compress()".I need to compress a file into a list of words from the user's input to recreate the original file. What does this mean?

Answer Source
import zlib

and do it to compress "file.txt"'s content, creating "compressedFile.zlib":

text = open("file.txt", "rb").read()
with open("compressedFile.zlib", "wb") as myFile:

and do it if you need to decompress "compressedFile.zlib" later and get its content:

compressedText = open("compressedFile.zlib", "rb").read()
decompressedText = zlib.decompress(compressedText)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download