0

Wondering what would be a good choice to store a binary data in file on a disk. Great if it would be a built-in Python module since I would like to keep it all stock. A random access to a written data could be a plus but it's not required. For this implementation I would rather go for a simplicity and speed. What I am looking for: save it now - get it later. Thanks in advance!

EDITED:

Found a problem why cPickle was erroring out. In one of the classes I have declared self.os=os And it appears self.os is not something cPickle likes... A bit later I found that cPickle doesn't accept PyQT objects if they (PyQT class instances) are given as a class's attributes (in case you dump a list of some_class instances).

An example below if run replicates the same error:

import cPickle
import os

class MyClass(object):
    """docstring for MyClass"""
    def __init__(self, arg):
        super(MyClass, self).__init__()
        self.arg = arg
        self.os=os         

data=MyClass("Hello World")    

file_name='dampData.data'

out_file = open(file_name, 'wb')
cPickle.dump(data, out_file)
out_file.close()

5 Answers 5

4

I would recommend cPickle - it is also built-in and significantly faster than pickle (in most cases).

example:

import cPickle

out_file = open(file_name, 'w')
cPickle.dump(data, out_file)
out_file.close()

in_file = open(file_name, 'r')
data = cPickle.load(in_file)
in_file .close()

From the official documentation of pickle:

The pickle module has an optimized cousin called the cPickle module. As its name implies, cPickle is written in C, so it can be up to 1000 times faster than pickle.

Sign up to request clarification or add additional context in comments.

6 Comments

While dampening a binary data (which is an instance of the class) cPickle gives this Error: raise TypeError, "can't pickle %s objects" % base.__name__ TypeError: can't pickle module objects
Which class are you dumping? have you tried: out_file = open(file_name, 'wb') in_file = open(file_name, 'rb') ?
Ive jist tried 'wb' flag instead of just 'w' while writing to a file. Unfortunately it did not fix the problem. cPickle refuses to dump. I have managed to make it dump a simple string variable. I opened a dump file made by cPickle and its content appeared to be a human readable. Are you sure cPickle works with a binary form?
What class is the object you dump?
Please see a response I appended to an original posting.
|
2

Take a look at pickle and shelve modules!

3 Comments

pickle complains it cannot dump a class's instance object TypeError: can't pickle module objects
@Sputnix seems that you pickling pickle module itself somehow, stackoverflow.com/questions/2790828/…
Please see a response I appended to an original posting.
0

You can use the normal python functions to read/write binary. Add a 'b' to the mode if on Windows:

f = open('workfile', 'wb') # opens a file for writing in binary mode

If you're using python 3, then you may need to do a little more work with string encodings.

Comments

0

Reading and Writing Files:

http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

You may also want to serialize your data so its easier to work with

http://docs.python.org/2/library/pickle.html

Comments

0

It's builtin.

f = open("somefile.zip", "rb")
g = open("thecopy.zip", "wb")

while True:
    buf = f.read(1024)
    if len(buf) == 0:
         break
    g.write(buf)

f.close()
g.close()

http://openbookproject.net/thinkcs/python/english3e/files.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.