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') 5 4808
"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
"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()
"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
"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
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 ' +...
|
by: Uwe Mayer |
last post by:
Hi,
is it possible to delete a file from a tar-archive using the tarfile module?
Thanks
Uwe
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |