[Tutor] Reading from a seperate file
Mark Lawrence
breamoreboy at yahoo.co.uk
Thu Oct 25 20:53:33 CEST 2012
On 25/10/2012 19:26, Joel Goldstick wrote:
> On Thu, Oct 25, 2012 at 2:15 PM, myles broomes
> <mylesbroomes at hotmail.co.uk> 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
>>
> check your spelling. check that you are in the same directory when
> your program runs.
>
>
If you've misspelt the filename or you're in the wrong directory surely
you'll get something like this depending on your OS.
c:\Users\Mark>MyPython\mytest.py
Traceback (most recent call last):
File "C:\Users\Mark\MyPython\mytest.py", line 2, in <module>
scoresFile=open('highScores.txt','r')
IOError: [Errno 2] No such file or directory: 'highScores.txt'
--
Cheers.
Mark Lawrence.
More information about the Tutor
mailing list