Problem Writing Files
Chris Gonnerman
chris.gonnerman at newcenturycomputers.net
Wed Apr 17 00:07:09 EDT 2002
----- Original Message -----
From: "Roger Jones" <rmj at slac.stanford.edu>
> I am using ActiveState Python 2.1 under Windows NT and after opening a
> file with:
>
> afile=('test.dat','w')
> afile.write('some random test text')
> afile.read()
This surely is not a screen capture from an actual session or a sample
of an actual script... the first line is obviously wrong, as it assigns
a tuple of ('test.dat', 'w') to afile rather than opening a file. It
would correctly be:
afile = open('test.dat', 'w')
Next, you write some data without a line ending (did you intend that?)
and then switched to using .read() on the file handle without further
action. Since you just did a write, the file pointer is at EOF, and
assuming it's OK to follow a .write() with a .read() you should get a
null (empty) string.
I don't know about NT but under many Unixoid OS's you must have an
intervening .seek() call between the .write() and .read() (including
of course a .rewind() call as it is basically an alias to .seek()).
Further the file mode 'w' should be 'w+' if you intend to both write
and read on the same stream.
Chris Gonnerman -- chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net
More information about the Python-list
mailing list