The bug in the reading between the lines


#1


Exercise 5 - reading between the lines. File does not exist bug

In what way does your code behave incorrectly? Include ALL error messages.

The file to be read text.txt in does not exist until after you have made a change to it.

My code:

import os
print os.listdir("./")
my_file = open("text.txt", "r")

print my_file.readline()
print my_file.readline()
print my_file.readline()

my_file.close()

The import os added to verify the bug. When you first load the exercise there are no files in the directory. If you run my code you get the following error:

Console output

[]
Traceback (most recent call last):
  File "python", line 3, in <module>
IOError: [Errno 2] No such file or directory: 'text.txt'

If you make any changes to the text.txt file the file will save and by listed in the os.dirlist("./") function, and the exact same code will work. (Additionally if you try to run code while the text.txt is focussed it will try to run the text file as python code)

What do you expect to happen instead?

The exercise to work without me having to look at the text file, or for the exercise to tell me I need to look at/change the text file.


#2

Thanks, @j-campbell, for the bug report.

Yeah, this bug has bugged many Codecademy users.

Another workaround is start off the code as follows, or with something similar, in order to create the text.txt file ...

f = open("text.txt", "w+")
f.write("Hello World!" + "\n")
f.write("Hello World!!" + "\n")
f.write("Hello World!!!" + "\n")
f.close()

Then add the code for opening, reading, and closing the file, as described in the instructions.


#3

One question I would have is,"how long is it taking to get from the first lesson to this one?"

Every time I start this module from lesson 1 and progress along, I never encounter this problem.


#4

I got it right away. Think I documented it okay? I went back after posting and had the same issue.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.