How to create new files?

Lee Harr missive at frontiernet.net
Thu Jul 12 18:11:10 EDT 2007


> 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'. I get an exception saying that the file doesn't
> exist. 

Works for me...

:~$ mkdir foo
:~$ cd foo
:foo$ ls
:foo$ python
Python 2.4.4 (#2, Apr  5 2007, 20:11:18)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('bar', 'w')
>>> f.write('test\n')
>>> f.close()
>>>
:foo$ ls
bar
:foo$ cat bar
test
:foo$


You're going to have to show us the actual code you used and
the actual error message you are getting.



> 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.

Are you trying to read in unicode?

Generally in python, you do not concern yourself with how much
space (how many bytes) a particular value takes up.

You may want to look at how the pickle module works.




More information about the Python-list mailing list