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?
Asked
Active
Viewed 1,460 times
0
-
1Please provide a [mcve] of what it is you are trying to do exactly. Show sample input, sample output, your own coding attempt and explain what exactly is not working in your implementation. – idjaw Sep 25 '16 at 13:23
-
I have no code yet as I have no idea how to go about this – codey Sep 25 '16 at 13:24
-
It is hard to provide advice for something that doesn't have a more detailed explanation as to what it is you are trying to do exactly. Read [this](http://stackoverflow.com/questions/4845339/how-to-compress-a-text). If that is what you are trying to do, great. If not, then please edit your question to provide a better [mcve] – idjaw Sep 25 '16 at 13:31
-
Its very simole to get code. Use this link for code https://www.google.co.in/search?q=compressing+file+in+python&oq=compressing+file+in+python&aqs=chrome..69i57j0.10666j0j4&client=ms-android-lenovo&sourceid=chrome-mobile&ie=UTF-8 – Dinesh Pundkar Sep 25 '16 at 13:32
1 Answers
0
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:
myFile.write(zlib.compress(text))
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)

Marcel
- 2,810
- 2
- 26
- 46