[Tutor] gzip

questions anon questions.anon at gmail.com
Mon Aug 8 01:57:51 CEST 2011


Thank you, I didn't realise that was all I needed.
Moving on to the next problem:
I would like to loop through a number of directories and decompress each
*.gz file and leave them in the same folder but the code I have written only
seems to focus on the last folder. Not sure where I have gone wrong.
Any feedback will be greatly appreciated.


import gzip
import os

MainFolder=r"D:/DSE_work/temp_samples/"

for (path, dirs, files) in os.walk(MainFolder):
    for dir in dirs:
        outputfolder=os.path.join(path,dir)
        print "the path and dirs are:", outputfolder
    for gzfiles in files:
        print gzfiles
        if gzfiles[-3:]=='.gz':
            print 'dealing with gzfiles:', dir, gzfiles
            f_in=os.path.join(outputfolder,gzfiles)
            print f_in
            compresseddata=gzip.GzipFile(f_in, "rb")
            newFile=compresseddata.read()
            f_out=open(f_in[:-3], "wb")
            f_out.write(newFile)


On Sun, Aug 7, 2011 at 11:22 AM, Walter Prins <wprins at gmail.com> wrote:

>
>
> On 7 August 2011 01:52, questions anon <questions.anon at gmail.com> wrote:
>
>> How can I output the decompressed file? something like:
>> output=file_content.write(filepath[:-3])
>>
>>
> See here:
> http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
> And here:
> http://docs.python.org/library/stdtypes.html#file.write
>
> Walter
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110808/e69b02b8/attachment.html>


More information about the Tutor mailing list