Compression in Python Programming Tutorialby Chris KeelingNovember 5, 201920 Comments Related posts: Python 3 Programming Tutorial – Functions Intro to Computer Science & Programming Using Python: 6.00.1x | MITx on edX | About Video What is the Best Programming Language to Learn First? Programming From Zero! Episode 1: Setting Up Your Compiler Tags:andcompresscompressionComputer Programming (Conference Subject)Computer Programming (Professional Field)Gzip (Software)How-to (Media Genre)Python (programming Language)Python (Software)thetutorialyouzlib 20 thoughts on “Compression in Python Programming Tutorial” mark Year December 8, 2013 at 6:57 pm i have a question. Do you know how to create a compressed file. For example in my class we have to get a file that has a text and get the frequency of each word and so and then compress the file. I was wondering if you could help me. i am just so lost. Reply burgie shr3d July 30, 2014 at 7:54 am The downside to using base64 output for the compression is that you don't get quite as good a compression ratio. To avoid using it – the file object used for writing the compressed data needs to be set to 'wb' (binary write), and 'rb' (binary read) when decompressing. In my testing: my decompressed file size was ~130 KB, base64 encoded compressed file size was ~80 KB, whereas in binary format, the compressed file size was ~60 KB. Examples: WRITING DATA plainData = open("decompressed.txt","r").read()compFile = open("compressed.txt","wb")compFile.write(zlib.compress(plainData)compFile.close() READING DATA compressedData = open("compressed.txt","rb").read()plainData = zlib.decompress(compressedData) Hope that helps 🙂 Reply Destrica UK June 6, 2015 at 3:34 pm Please could you help me, I get the following error: Compression: Raw Size: 104270Traceback (most recent call last): File "C:/Users/Emma/Desktop/Compression Algorithm.py", line 25, in <module> end() File "C:/Users/Emma/Desktop/Compression Algorithm.py", line 23, in end main() File "C:/Users/Emma/Desktop/Compression Algorithm.py", line 8, in main compressed = zlib.compress(text, 9)TypeError: 'str' does not support the buffer interface>>> For this code: import time, base64, sys, random, winsound, doctest, urllib, math, zlib; from tkinter import *#start def main(): text = open("sample.txt", "r").read() print("Raw Size: ", sys.getsizeof(text)) compressed = zlib.compress(text, 9) print("9 Compressed size: ", sys.getsizeof(compressed)) savecomp = open("compdata.txt", "a") savecomp.write(compressed) savecomp.close() print(compressed) decompressed = zlib.decompress(compressed) print(decompressed) def end(): print("Compression: ") print() time.sleep(1) main() end()#end Reply GlennBen May 20, 2016 at 6:01 pm I would like to learn how to write my own algorithm for compressing files, but I don't know what technologies to learn. In simpler terms what do I have to learn to write my own compressing algorithms like zlib, if that makes sense? Like how do I create another version of zlib? Reply Self Betterment June 6, 2016 at 4:34 pm Could you please help me with this error, when I try and run your code it does this.compression:import zlib,sys,time,base64text = open('file.txt','w').read()print 'raw size:' ,sys.getsizeof(text)compressed = zlib.compress(text,9)print '9 compressed siz:',sys.getsizeof(compressed)savecomp = open('compdata.txt','a')savecomp.write(compressed)savecomp.close()that is the code by it says syntax error on the 2nd apostrophe on the line print 'raw size:' ,sys.getsizeof(text) Reply solitariobrz June 22, 2016 at 4:08 pm I'm here because of Pied Piper 😉 Reply SgtPepper September 2, 2016 at 4:07 pm what version of python are you using? Reply Harambe September 25, 2016 at 2:09 pm Who's here because your shitty school is making you watch this? Reply Yuva Raja October 9, 2016 at 7:36 pm could you also do videos for data structures,algorithms –basics,intermediate in python ? Reply G M Prashanth January 30, 2017 at 6:53 am Please could you help me I get the following error:Traceback (most recent call last): File "C:UsersPrashanth GMAppDataLocalProgramsPythonPython36comp.py", line 7, in <module> compressed = base64.b64encode(zlib.compress(text))TypeError: a bytes-like object is required, not 'str' I am Using Python 3.6.0 Shell My Code is import zlibimport sysimport timeimport base64 text= "compression example with encoding"compressed = base64.b64encode(zlib.compress(text))print (compressed) output = open("encodecomp.txt","a")output.write(compressed)output.close() readFile = open("encodecomp.txt","r").read() decompressed = zlib.decompress(base64.b64decode(readFile)) Reply harry howes March 29, 2017 at 2:32 pm What version of python is this? Reply Kostas Nikolouts September 7, 2017 at 11:42 pm Can we compress a file then compress the compressed and so on…?Also when I tried to compress a sting I got an error who said that I can compress only bytes-like objects.What it is going on? Reply Shritej Thorve October 26, 2017 at 8:19 pm I am getting sample cod e from ur website Reply Stefan March 30, 2019 at 10:42 pm If anybody is getting the error message: TypeError: a bytes-like object is required, not 'str' You need to convert the text file into bytes: text_string = open('compress_me.txt','r').read() text_bytes = text_string.encode("utf-8") compressed = zlib.compress(text_bytes,9) print("9 compressed size:",sys.getsizeof(compressed)) Reply Newton Tavengwa July 27, 2019 at 11:49 am inspired by pied piper to search about compression Reply Biru Singh August 5, 2019 at 6:10 am Can i use this for compressing images ? Reply Shivam Bhirud August 11, 2019 at 10:39 am can we use multithreading to make the compression faster somehow? Reply Niyanta Kapadia September 11, 2019 at 1:15 am Is there any way to reduce the size of all types of file using a single python code ? (String,audio,video and image) Reply dafa geraldine October 6, 2019 at 12:42 am hey dude, thanks for your tutorial video.but i have a question, when i try compressing and decompressing a file without base64, its work finely. and also when i use base64 it work finely too. do u have opinion about this ?? Reply otacon1024 October 16, 2019 at 3:27 pm Here because I for some reason decided to run a website that archives and provides an API for full historical WoW auction house snapshots across all regions. Realized I probably bit off more than I could chew when I gathered some data for sampling and realized that storing it as received would generate about 1.6 TB of data monthly xDSimply restructuring the data got me to about 10% of that. Pushing the result through 7zip on Ultra nets me 20% of that , so I'm left with something that's 2% the original size, which is pretty impressive. Will be pretty interesting to see how Python libraries compare to 7zip.Anyways, compression = epicly useful stuff. UPDATE: Well, after some testing, zlib underperformed 7zip (was getting about 3.5% overall). The algorithm I used with 7zip was LZMA, so I did a bit of searching, and sure enough that also ships with Python, and got me the same 2% overall compression I had with 7zip. So check that one out if you really need to squeeze out every last byte out of that data. Documentation here: https://docs.python.org/dev/library/lzma.html Reply Leave a Reply Cancel reply Your email address will not be published. Required fields are marked *Comment Name * Email * Save my name, email, and website in this browser for the next time I comment.