On 26/10/12 05:15, myles broomes wrote:

I'm trying to code a program that retrieves data from a seperate
file but according to my program, the seperate file is empty when
I know it clearly isn't. It's a txt file and here are its contents:
 120
74
57
44
12
I thought that maybe the problem was the code I had written but
even when I try and read from the file in an interactive session
in the Python Shell, it does the same thing. I open it in read mode
and assign to a variable like so: scoresFile=open('highScores.txt','r')
But whenever I try to say read from it: scoresFile.read() It comes
up with a blank string: ' ' Can anyone help me?
Myles Broomes


The symptoms you describe suggest that you are reading from the file
twice without closing the file first, or resetting the file pointer.
Once the file pointer reaches the end of the file, there's nothing
left to read and you get an empty string.

Example:

py> count = open("demo", "w").write("some text")
py> f = open("demo", "r")
py> f.read()
'some text'
py> f.read()
''
py> f.read()
''
py> f.seek(0)  # move the file pointer back to the start
0
py> f.read()
'some text'



--
Steven

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to