[Tutor] Reading individual file from tar file as a file object

xbmuncher xboxmuncher at gmail.com
Thu Oct 8 22:10:58 CEST 2009


I have a large tar.bz2 file that I want to extract certain files directly to
an FTP path.
Since the extract() method doesn't accept ftp paths...  I wanted to read the
files from the tar file like a file object or file I/O stream.
Is there a way to do this?
Here's my pseudocode:

import tarfile

def putThisDataSomewhere(data):
    #write it to a file in FTP


tar = tarfile.open("HUGE_FILE_OVER_5GB.tar.bz2", "r:bz2")
readSize = 50

for tarInfo in tar:
    fileSize = tarInfo.size

    #prepare for loop to read specific file inside tar
    if readSize > fileSize
        readSize = fileSize
    readIterations = fileSize/readSize

    #loop & read file
    for i in range(readIterations):
        putThisDataSomewhere(tarFile.read(readSize))

    #catch remainder of file
    lastReadSize = fileSize - (readSize * readIterations)
    if lastReadSize:
        putThisDataSomewhere(tarFile.read(lastReadSize))



tar.close()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091008/a1aef4c3/attachment-0001.htm>


More information about the Tutor mailing list