how exactly do binary files work in python?

Erik Max Francis max at alcyone.com
Sun Mar 12 19:35:47 EST 2006


John Salerno wrote:

> In C#, writing to a binary file wrote the actual data types into the 
> file (integers, etc.).

This was inherently nonportable.

> Is this not how Python binary files work? I tried 
> to write integers into a file, but the write method only takes a string 
> argument anyway.
> 
> Is there a way to actually store integers in a file, so that they can be 
> read and used (added, compared, etc.) as integers?

You can use the struct module for converting fundamental types to a 
portable string representation for writing to binary files.  Since 
you're dealing with a high-level language, you can also just use the 
pickle module for a more general form of serialization and persistence.

-- 
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
   My reputation grows with every failure.
   -- George Bernard Shaw



More information about the Python-list mailing list