[Tutor] write problem

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 10 Jan 2002 11:43:18 -0800 (PST)


On Thu, 10 Jan 2002, Ron wrote:

> Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> IDLE 0.8 -- press F1 for help
> >>> data = open("ron.txt","a")
> >>> data.write("help me!")
> >>> data.read()
> '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

Hi Ron,

The file position is at the end of the file, so you won't be able to read
what you've immediately written until you rewind the file.  What you'll
probably want to do is rewind or "seek()" back to the beginning of the
file:

###
data.seek(0)
###

This will set the file position back to the very beginning.


One other thing you may need to do is tell the system that you intend to
both read and write on the file.  'a' mode is only meant for writing, and
certain systems may not like it if you try reading from the file.

Use "a+" mode instead:

###
data = open("ron.txt", "a+")
###


Hope this helps!