[Tutor] Reading individual file from tar file as a file object
Kent Johnson
kent37 at tds.net
Fri Oct 9 00:17:39 CEST 2009
On Thu, Oct 8, 2009 at 4:10 PM, xbmuncher <xboxmuncher at gmail.com> wrote:
> 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?
I think tarfile.extractfile() does what you want. The docs are a bit
confusing but it returns a file-like object from which the item can be
read. Your code would be something like
tar = tarfile.open("HUGE_FILE_OVER_5GB.tar.bz2", "r:bz2")
for tarInfo in tar:
putThisDataSomewhere(tar.extractfile(tarInfo))
putThisDataSomewhere() will read from its argument and write to FTP.
Any required blocking or buffering would be in putThisDataSomewhere().
Kent
More information about the Tutor
mailing list