[Tutor] Reading gzip files
David
david at abbottdavid.com
Mon Dec 1 02:17:00 CET 2008
Dinesh B Vadhia wrote:
> I'm reading gzip files and writing the content out to a text file line
> by line. The code is simply:
>
> import gzip
> list_zipfiles = dircache.listdir(zipfolder)
> writefile = "out_file.txt"
> fw = open(writefile, 'w')
>
> for ziparchive in list_zipfiles:
> zfile = gzip.GzipFile(zipfolder + ziparchive, "r")
> for line in zfile:
> fw.write(line)
> zfile.close()
> fw.close()
I am learning also. I came up with this;
#!/usr/bin/python
import tarfile
tFile = tarfile.open("/home/david/zip_files/zip.tar.gz", "r")
for f in tFile.getnames():
print f
tFile.close()
#fname = "out.txt"
#fobj = open(fname, 'w')
#for line in f:
# fobj.write(line + '/n')
#tFile.close()
#fobj.close()
My problem is I can not figure out how to write to the file with new
lines, as you can see my attempts at the bottom. This works fine in Linux;
./py_list_zip.py >> out.txt
but I want to learn how to do it within python, plus to understand.
any hints :)
thanks
-david
--
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com
More information about the Tutor
mailing list