How to create new files?

Hrvoje Niksic hniksic at xemacs.org
Fri Jul 13 03:38:06 EDT 2007


Robert Dailey <rcdailey at gmail.com> writes:

> class filestream:
> 	def __init__( self, filename ):
> 		self.m_file = open( filename, "rwb" )
[...]
> So far, I've found that unlike with the C++ version of fopen(), the
> Python 'open()' call does not create the file for you when opened
> using the mode 'w'.

According to your code, you're not using 'w', you're using 'rwb'.  In
that respect Python's open behaves the same as C's fopen.

> Also, you might notice that my "self.m_file.read()" function is wrong,
> according to the python docs at least. read() takes the number of
> bytes to read, however I was not able to find a C++ equivalent of
> "sizeof()" in Python. If I wanted to read in a 1 byte, 2 byte, or 4
> byte value from data into python I have no idea how I would do this.

Simply read as much data as you need.  If you need to unpack external
data into Python object and vice versa, look at the struct module
(http://docs.python.org/lib/module-struct.html).



More information about the Python-list mailing list