472,954 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

tarfile's tar.extractfile() file-like object incompatible with pickle.load()?

Hi everyone! I must be doing something wrong here :) I have a
tarball that contains a single file whose contents are a pickled
object. I would like to unpickle the object directly from the tarball
using the file-like object provided by extractfile(). Attempts to do
this result in EOFError. However if I first extract to a temporary
file, then unpickle from there, it works. The below code reproduces
the problem (on my machine at least). I'm running Python 2.3.4,
manually installed on Debian Woody (original python removed). Thanks!

This sample code creates (and then removes) files in the tmp directory
and in the current working directory.

# demonstrates extractfile/unpickle failure (bug?)

# pickle a dict to a temp file
# create tar file, add temp file to it, close tar file
# open tar file for reading
# obtain file-like object for pickled file using extractfile()
# attempt to unpickle dict from file-like object
# fails with EOFError exception

import tarfile
import pickle
import tempfile
import os

if __name__ == '__main__':
try:
hashtopickle = { 'a' : 1, 'b' : 2 }

# pickle to temp file
(fd, tmpfilename) = tempfile.mkstemp()
tmpfile = os.fdopen(fd, 'w')
pickle.dump(hashtopickle, tmpfile)
tmpfile.close()

# create tar; add temp file
tar = tarfile.open('tarpickle.tar', 'w')
tar.add(tmpfilename, 'pickledhash')
tar.close()

# remove temp file
os.remove(tmpfilename)

# open tarfile for reading, get filelike
tar = tarfile.open('tarpickle.tar', 'r')
filelike = tar.extractfile('pickledhash')

# fails
hashcopy = pickle.load(filelike)

finally:
# cleanup
os.remove('tarpickle.tar')
Jul 18 '05 #1
5 4707

"Matt Doucleff" <ma******@ugcs.caltech.edu> wrote in message
news:3e**************************@posting.google.c om...
Hi everyone! I must be doing something wrong here :) I have a
tarball that contains a single file whose contents are a pickled
object. I would like to unpickle the object directly from the tarball
using the file-like object provided by extractfile(). Attempts to do
this result in EOFError. However if I first extract to a temporary
file, then unpickle from there, it works. The below code reproduces
the problem (on my machine at least). I'm running Python 2.3.4,
manually installed on Debian Woody (original python removed). Thanks!

This sample code creates (and then removes) files in the tmp directory
and in the current working directory.

# demonstrates extractfile/unpickle failure (bug?)

# pickle a dict to a temp file
# create tar file, add temp file to it, close tar file
# open tar file for reading
# obtain file-like object for pickled file using extractfile()
# attempt to unpickle dict from file-like object
# fails with EOFError exception

import tarfile
import pickle
import tempfile
import os

if __name__ == '__main__':
try:
hashtopickle = { 'a' : 1, 'b' : 2 }

# pickle to temp file
(fd, tmpfilename) = tempfile.mkstemp()
tmpfile = os.fdopen(fd, 'w')
pickle.dump(hashtopickle, tmpfile)
tmpfile.close()

# create tar; add temp file
tar = tarfile.open('tarpickle.tar', 'w')
tar.add(tmpfilename, 'pickledhash')
tar.close()

# remove temp file
os.remove(tmpfilename)

# open tarfile for reading, get filelike
tar = tarfile.open('tarpickle.tar', 'r')
filelike = tar.extractfile('pickledhash')

# fails
hashcopy = pickle.load(filelike)

finally:
# cleanup
os.remove('tarpickle.tar')


Maby you should,

import StringIO

hashcopy = pickle.load(StringIO.StringIO(filelike))

but im not sure,

Tom
Jul 18 '05 #2

"Matt Doucleff" <ma******@ugcs.caltech.edu> wrote in message
news:3e**************************@posting.google.c om...
Hi everyone! I must be doing something wrong here :) I have a
tarball that contains a single file whose contents are a pickled
object. I would like to unpickle the object directly from the tarball
using the file-like object provided by extractfile(). Attempts to do
this result in EOFError. However if I first extract to a temporary
file, then unpickle from there, it works. The below code reproduces
the problem (on my machine at least). I'm running Python 2.3.4,
manually installed on Debian Woody (original python removed). Thanks!

This sample code creates (and then removes) files in the tmp directory
and in the current working directory.

# demonstrates extractfile/unpickle failure (bug?)

# pickle a dict to a temp file
# create tar file, add temp file to it, close tar file
# open tar file for reading
# obtain file-like object for pickled file using extractfile()
# attempt to unpickle dict from file-like object
# fails with EOFError exception

import tarfile
import pickle
import tempfile
import os

if __name__ == '__main__':
try:
hashtopickle = { 'a' : 1, 'b' : 2 }

# pickle to temp file
(fd, tmpfilename) = tempfile.mkstemp()
tmpfile = os.fdopen(fd, 'w')
pickle.dump(hashtopickle, tmpfile)
tmpfile.close()

# create tar; add temp file
tar = tarfile.open('tarpickle.tar', 'w')
tar.add(tmpfilename, 'pickledhash')
tar.close()

# remove temp file
os.remove(tmpfilename)

# open tarfile for reading, get filelike
tar = tarfile.open('tarpickle.tar', 'r')
filelike = tar.extractfile('pickledhash')

# fails
hashcopy = pickle.load(filelike)

finally:
# cleanup
os.remove('tarpickle.tar')


It occurs to me that you need to do,

hashcopy = pickle.loads(filelike)

if filelike is a string.

Tom
P.S. have a look at pickle.dumps()
Jul 18 '05 #3
"Tom B." <sb******@commspeed.net> wrote in message news:<10***************@news.commspeed.net>...
"Matt Doucleff" <ma******@ugcs.caltech.edu> wrote in message
news:3e**************************@posting.google.c om...
Hi everyone! I must be doing something wrong here :) I have a
tarball that contains a single file whose contents are a pickled
object. I would like to unpickle the object directly from the tarball
using the file-like object provided by extractfile(). Attempts to do
this result in EOFError. However if I first extract to a temporary
file, then unpickle from there, it works. The below code reproduces
the problem (on my machine at least). I'm running Python 2.3.4,
manually installed on Debian Woody (original python removed). Thanks!

This sample code creates (and then removes) files in the tmp directory
and in the current working directory.

# demonstrates extractfile/unpickle failure (bug?)

# pickle a dict to a temp file
# create tar file, add temp file to it, close tar file
# open tar file for reading
# obtain file-like object for pickled file using extractfile()
# attempt to unpickle dict from file-like object
# fails with EOFError exception

import tarfile
import pickle
import tempfile
import os

if __name__ == '__main__':
try:
hashtopickle = { 'a' : 1, 'b' : 2 }

# pickle to temp file
(fd, tmpfilename) = tempfile.mkstemp()
tmpfile = os.fdopen(fd, 'w')
pickle.dump(hashtopickle, tmpfile)
tmpfile.close()

# create tar; add temp file
tar = tarfile.open('tarpickle.tar', 'w')
tar.add(tmpfilename, 'pickledhash')
tar.close()

# remove temp file
os.remove(tmpfilename)

# open tarfile for reading, get filelike
tar = tarfile.open('tarpickle.tar', 'r') filelike = tar.extractfile('pickledhash')

# fails
hashcopy = pickle.load(filelike)

finally:
# cleanup
os.remove('tarpickle.tar')


It occurs to me that you need to do,

hashcopy = pickle.loads(filelike)

if filelike is a string.

Tom
P.S. have a look at pickle.dumps()


The tarfile.extractfile() method does not read the contents
of the encapsulated file into a string, but constructs a new
object that implements file operations (it is like a file)
and is intended to be used as if you had simply opened the
tar-encapsulated file directly.

Matt
Jul 18 '05 #4

"Matt Doucleff" <ma******@ugcs.caltech.edu> wrote in message
news:3e**************************@posting.google.c om...
"Tom B." <sb******@commspeed.net> wrote in message

news:<10***************@news.commspeed.net>...
"Matt Doucleff" <ma******@ugcs.caltech.edu> wrote in message
news:3e**************************@posting.google.c om...
Hi everyone! I must be doing something wrong here :) I have a
tarball that contains a single file whose contents are a pickled
object. I would like to unpickle the object directly from the tarball
using the file-like object provided by extractfile(). Attempts to do
this result in EOFError. However if I first extract to a temporary
file, then unpickle from there, it works. The below code reproduces
the problem (on my machine at least). I'm running Python 2.3.4,
manually installed on Debian Woody (original python removed). Thanks!

This sample code creates (and then removes) files in the tmp directory
and in the current working directory.

# demonstrates extractfile/unpickle failure (bug?)

# pickle a dict to a temp file
# create tar file, add temp file to it, close tar file
# open tar file for reading
# obtain file-like object for pickled file using extractfile()
# attempt to unpickle dict from file-like object
# fails with EOFError exception

import tarfile
import pickle
import tempfile
import os

if __name__ == '__main__':
try:
hashtopickle = { 'a' : 1, 'b' : 2 }

# pickle to temp file
(fd, tmpfilename) = tempfile.mkstemp()
tmpfile = os.fdopen(fd, 'w')
pickle.dump(hashtopickle, tmpfile)
tmpfile.close()

# create tar; add temp file
tar = tarfile.open('tarpickle.tar', 'w')
tar.add(tmpfilename, 'pickledhash')
tar.close()

# remove temp file
os.remove(tmpfilename)

# open tarfile for reading, get filelike
tar = tarfile.open('tarpickle.tar', 'r') filelike = tar.extractfile('pickledhash')

# fails
hashcopy = pickle.load(filelike)

finally:
# cleanup
os.remove('tarpickle.tar')


It occurs to me that you need to do,

hashcopy = pickle.loads(filelike)

if filelike is a string.

Tom
P.S. have a look at pickle.dumps()


The tarfile.extractfile() method does not read the contents
of the encapsulated file into a string, but constructs a new
object that implements file operations (it is like a file)
and is intended to be used as if you had simply opened the
tar-encapsulated file directly.

Matt


How about,

filelike = tar.extractfile('pickledhash')
filetext = filelike.read()
hashcopy = pickle.load(StringIO.StringIO(filetext ))

Tom
Jul 18 '05 #5

"Matt Doucleff" <ma******@ugcs.caltech.edu> wrote in message
news:3e**************************@posting.google.c om...
"Tom B." <sb******@commspeed.net> wrote in message

news:<10***************@news.commspeed.net>...
"Matt Doucleff" <ma******@ugcs.caltech.edu> wrote in message
news:3e**************************@posting.google.c om...
Hi everyone! I must be doing something wrong here :) I have a
tarball that contains a single file whose contents are a pickled
object. I would like to unpickle the object directly from the tarball
using the file-like object provided by extractfile(). Attempts to do
this result in EOFError. However if I first extract to a temporary
file, then unpickle from there, it works. The below code reproduces
the problem (on my machine at least). I'm running Python 2.3.4,
manually installed on Debian Woody (original python removed). Thanks!

This sample code creates (and then removes) files in the tmp directory
and in the current working directory.

# demonstrates extractfile/unpickle failure (bug?)

# pickle a dict to a temp file
# create tar file, add temp file to it, close tar file
# open tar file for reading
# obtain file-like object for pickled file using extractfile()
# attempt to unpickle dict from file-like object
# fails with EOFError exception

import tarfile
import pickle
import tempfile
import os
Now that I take a closer look at your program it should read,

if __name__ == '__main__':
try:
hashtopickle = { 'a' : 1, 'b' : 2 }

# pickle to temp file
(fd, tmpfilename) = tempfile.mkstemp()
tmpfile = file(tmpfilename,'w+')
pickle.dump(hashtopickle, tmpfile)
tmpfile.close()

# create tar; add temp file
tar = tarfile.open('tarpickle.tar', 'w')
tar.add(tmpfilename, 'pickledhash')
tar.close()

# remove temp file
os.remove(tmpfilename)

# open tarfile for reading, get filelike
tar = tarfile.open('tarpickle.tar', 'r') filelike = tar.extractfile('pickledhash')

# fails
hashcopy = pickle.load(filelike)

finally:
# cleanup
os.remove('tarpickle.tar')

Your opening the tmpfilename in an odd way.

Tom
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Lars Behrens | last post by:
Hi, Pythonistas! I'm quite new to Python and have a problem with a simple backup script. This code: tar = tarfile.open('/home/lars/test.tar.gz', 'w:gz') tar.addfile('/home/lars') brings...
8
by: Jay Donnell | last post by:
Is there a way to use the tarfile module to recursively compress the contents of a directory and maintain the directory structure in the tar archive? Simply doing os.system('tar -czvf ' +...
5
by: Uwe Mayer | last post by:
Hi, is it possible to delete a file from a tar-archive using the tarfile module? Thanks Uwe
1
by: Beowulf | last post by:
Hello, I'm using Python to automate admin tasks on my job. We use Windoze 2000 as desktop platform. When executing this daily backup scripts I get the following error: Traceback (most recent...
3
by: N. Volbers | last post by:
Hello everyone, I noticed that when you open a zipped tarball using 'tarfile' and if you then get the pseudo-file descriptor fd for a file via 'extractfile', then fd.tell() is broken in the...
4
by: Claudio Grondi | last post by:
I need to unpack on a Windows 2000 machine some Wikipedia media .tar archives which are compressed with TAR 1.14 (support for long file names and maybe some other features) . It seems, that...
3
by: justin.vanwinkle | last post by:
Hello everyone, I need some tar functionality for my program. Currently the following code properly displays tar archives, and tar.gz archives. However, it chokes on tar.bz2 archives with the...
3
by: Anurag | last post by:
Hi, I am trying to use tarfile module to list contents of a 'gz' file but it seems to hang for large files and CPU usage goes 100%. though 'tar -tvf' on same file list contents in couple of...
6
by: Terry Carroll | last post by:
I am trying to do something with a very large tarfile from within Python, and am running into memory constraints. The tarfile in question is a 4-gigabyte datafile from freedb.org,...
1
by: boblatest | last post by:
Hello, I'm trying to catch an "EOFError" exception that occurs when reading truncated tarfile. Here's my routine, and below that the callback trace. Note that although I'm trying to catch all...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.