[Tutor] read and write data??

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Aug 4 01:50:17 CEST 2004



On Tue, 3 Aug 2004, jason hochstein wrote:

> I cant figure out how to write data into or read data out of a file. I
> keep getting errors saying the file I create doesn't exist, even though
> I see it in my saved docs. What am I doing wrong?


Hi Jason,

We need a little more information.  Can you show us what the literal error
message is?  Also, show us the snippet of code that you're using to open()
your file.

It's usually best to try to send us enough information so that we can try
to duplicate the problem that you're getting.  And when you get
exceptions, copy-and-paste the error message; error messages often have
very subtle clues as to what's going on.

The problem might be as simple as a backslash-escaping issue (if you're on
Windows), or opening a file in read-mode instead of write mode.  We can't
what's going on without more information, though.



Here's a small example of opening and closing files:

###
>>>
>>> f = open('message', 'w')
>>> f.write("This is a message")
>>> f.write("do you see this?")
>>> f.close()
>>>
>>>
>>> f = open('message')
>>> for line in f:
...     print line.upper()
...
THIS IS A MESSAGEDO YOU SEE THIS?
###


Hope this helps!



More information about the Tutor mailing list