Article View: pl.comp.lang.python
Article #180Re: gzip
From: news.tpi.pl
Date: Wed, 04 Dec 2002 11:42
Date: Wed, 04 Dec 2002 11:42
89 lines
4055 bytes
4055 bytes
"Pawel J Maczewski" <pjmacz@slcj.uw.edu.pl> wrote in message
news:ask7ug$8ko$3@news.tpi.pl...
> Czesc,
> Kolejne moje glupiutkie pytanie: chcialbym strukture danych ktora
> zapisuje do pliku compresowac (oszczednosc miejsca w moim przypadku ok
> 80%). Wiec mam pytanie: czy istnieje jakis modul to umozliwiajacy
> (to=kompresje i dekompresje) bezposrednio z poziomu pythona? czy tez mam
> import os i przez wywolanie komend systemowych dzialac? ;)
To jest bardzo przydatna klasa. Pracujesz z nia jak z normalnym
plikiem. Tyle ze skompresowanym.
# Text files with automatic (un)compression
#
# Written by: Konrad Hinsen <hinsenk@ere.umontreal.ca>
# Last revision: 1996-11-8
#
# Modified by: Rafal Smotrzyk <rsmotrzyk@mikroplan.com.pl>
# Tested: python 1.5, WinNT 4.0 SP3
# Last revision: 1998-12-16
# - added .wrz files
# - standard module gzip
# - removed file closing from destructor
# Last revision: 1998-12-31
# - added 'read' method
# This module defines a class TextFile whose instances can be
# accessed like normal file objects (i.e. by calling readline(),
# readlines(), and write()), but can also be used as line iterators
# in for loops.
#
# The class TextFile also handles compression transparently, i.e. it is
# possible to read lines from a compressed text file as if it were not
# compressed. Compression is deduced from the file name suffixes '.Z'
# (compress/uncompress) and '.gz' (gzip/gunzip) and '.wrz' and '.tgz'.
import os
import gzip
class TextFile:
def __init__(self, filename, mode = 'r'):
adir,afname=os.path.split(filename)
self.myfileobj=None
if mode == 'r':
if not os.path.exists(filename):
raise IOError, (2, 'No such file or directory')
if filename[-3:]=='.gz' or filename[-4:]=='.wrz' or
filename[-4:]=='.tgz':
self.myfileobj = open(filename, mode+'b')
self.file=gzip.GzipFile(afname,mode+'b',9,self.myfileobj)
else:
self.file = open(filename, mode)
elif mode == 'w':
if filename[-3:]=='.gz' or filename[-4:]=='.wrz' or
filename[-4:]=='.tgz':
self.myfileobj = open(filename, mode+'b')
self.file=gzip.GzipFile(afname,mode+'b',9,self.myfileobj)
else:
self.file = open(filename, mode)
else:
raise IOError, (0, 'Illegal mode')
def __getitem__(self, item):
line = self.file.readline()
if not line:
raise IndexError
return line
def read(self,size=-1):
return self.file.read(size)
def readline(self):
return self.file.readline()
def readlines(self):
return self.file.readlines()
def write(self, data):
self.file.write(data)
def writelines(self, list):
for line in list:
self.file.write(line)
def tell(self):
if not self.myfileobj is None:
return self.myfileobj.tell()
return self.file.tell()
def close(self):
if not self.file is None:
self.file.close()
self.file=None
if not self.myfileobj is None:
self.myfileobj.close()
self.myfileobj=None
Technical Details
Message-ID:
<askplc$1mm$1@news.tpi.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!news.onet.pl!newsfeed.tpinternet.pl!news.tpi.pl!not-for-mailReferences:
<ask7ug$8ko$3@news.tpi.pl>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!pingwin.icm.edu.pl!news.icm.edu.pl!news.onet.pl!newsfeed.tpinternet.pl!news.tpi.pl!not-for-mail From: "news.tpi.pl" <rav@smo3k.z.pl> Newsgroups: pl.comp.lang.python Subject: Re: gzip Date: Wed, 4 Dec 2002 12:42:47 +0100 Organization: tp.internet - http://www.tpi.pl/ Lines: 92 Message-ID: <askplc$1mm$1@news.tpi.pl> References: <ask7ug$8ko$3@news.tpi.pl> NNTP-Posting-Host: mpip24.mikroplan.com.pl X-Trace: news.tpi.pl 1039002094 1750 195.117.158.24 (4 Dec 2002 11:41:34 GMT) X-Complaints-To: usenet@tpi.pl NNTP-Posting-Date: Wed, 4 Dec 2002 11:41:34 +0000 (UTC) X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MSMail-Priority: Normal X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit