Tarfile_Extracting file from Archive














































Tarfile_Extracting file from Archive



Extracting file from an Archive

To access the data from an archive member within your program, use the extractfile() method, passing the member's name.

extractfile()-:  
                     Extract a member from the archive as a file object. member may be a filename or a TarInfo object. If member is a regular file or a link, an io.BufferedReader object is returned. Otherwise, None is returned.

io.BufferedReader-:

  • A buffer providing higher-level access to a readable, sequential RawIOBase object. It inherits BufferedIOBase. When reading data from this object, a larger amount of data may be requested from the underlying raw stream, and kept in an internal buffer. The buffered data can then be returned directly on subsequent reads.



Examples-:

import tarfile

t = tarfile.open(
'tarfile_add.tar', 'r')
for filename in [ 'README.txt', 'notthere.txt' ]:
try:
f = t.extractfile(filename)
except KeyError:
print 'ERROR: Did not find %s in tar archive' % filename
else:
print filename, ':', f.read()

Output:-



import tarfile
import os

os.mkdir('outdir')
t = tarfile.open('tarfile_add.tar', 'r')
t.extractall('outdir')
print (os.listdir('outdir'))

Output:-




Comments