alphanumeric
alphanumeric

Reputation: 19349

Python Storing a Binary Data in File on disk

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()

Upvotes: 0

Views: 2194

Answers (5)

zvisofer
zvisofer

Reputation: 1368

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.

Upvotes: 4

joel3000
joel3000

Reputation: 1329

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

Upvotes: 0

CaffeineAddiction
CaffeineAddiction

Reputation: 823

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

Upvotes: 0

chromic
chromic

Reputation: 61

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.

Upvotes: 0

ndpu
ndpu

Reputation: 22571

Take a look at pickle and shelve modules!

Upvotes: 2

Related Questions