tarfile : read from a socket?

Lars Gustäbel lars at gustaebel.de
Fri Feb 12 03:40:38 EST 2016


On Fri, Feb 12, 2016 at 09:35:40AM +0100, Antoon Pardon wrote:
> On 02/11/2016 06:27 PM, Lars Gustäbel wrote:
> > What about using an iterator?
> >
> > def myiter(tar):
> >     for t in tar:
> >         print "extracting", t.name
> >         yield t
> >
> > sfo = sock.makefile('r')
> > taro = tarfile.open(fileobj=sfo,mode='r|')
> > taro.extractall(members=myiter(taro),path=edir)
> >
> The tarfile is already an iterator. Just do the following: for ti in
> taro:     print "extracting", ti.name taro.extract(ti)

The extractall() method does a little bit more than just extract(), i.e.
setting directory mtimes, see
https://docs.python.org/3.5/library/tarfile.html#tarfile.TarFile.extractall

-- 
Lars Gustäbel
lars at gustaebel.de


More information about the Python-list mailing list