vendredi 1 juillet 2016

How to compress a text file in Python? [on hold]

I have a text file created and I want to compress it.

How would I accomplish this?

I have done some research, around the forum ; found a question, similar to this but when I tried it out, it did not work as it was text typed in, not a file, for example

import zlib, base64
text = 'STACK OVERFLOW'
code =  base64.b64encode(zlib.compress(text,9))
print code

source from: (Compressing a file in python and keep the grammar exact when opening it again)

When i tried it out this error came up, for example:

hTraceback (most recent call last):
File "C:UsersShahidDesktopSuhailTask 3.py", line 3, in <module>
code =  base64.b64encode(zlib.compress(text,9))
TypeError: must be string or read-only buffer, not file

Here is the code that I have used:

import zlib, base64
text = open('Suitable.txt','r')
code =  base64.b64encode(zlib.compress(text,9))
print code

But what i want is a text file to be compressed.

Aucun commentaire:

Enregistrer un commentaire