5/9 Reading Between the Lines


This is totally weird. Is there a bug or something? :confounded:

# Add your code here!

my_file = open("text.txt", "r")

print my_file.readline()

print my_file.readline()

print my_file.readline()


Codecademy says:

Oops, try again. Sorry, but I'm gonna need you to refresh the page! Don't worry your code is saved.

I have refreshed the page time and again, but it keeps on repeating.

Console prints:

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

tks for your help. :slight_smile:


my_file = open("text.txt","r")

you are trying to open an existing file.....in read modus.....
If you use following code
you will see which files are available....

import os
print "==show us the existing files =="
files = [f for f in os.listdir('.') if os.path.isfile(f)]
for f in files:
    print str(f)

As a solution to the file not being there

import os
print "== if the file doesn't exist, create one =="
if os.path.exists('text.txt'):
    print "File exists"
    print "Creating the text.txt-file"
    my_file = open("text.txt", 'w')
    my_file.write("I'm the first line of the file!" +"\n")
    my_file.write("I'm the second line."+"\n")
    my_file.write("Third line here, boss."+"\n")

my_file = open("text.txt","r")
print my_file.readline()
print my_file.readline()
print my_file.readline()

== discussions / opinions ==


WOW! I didn't know it was this complicated! Tks a lot! :smile:


I had this exact same problem. I saw https://discuss.codecademy.com/t/5-9-reading-between-the-lines-error/42558/5 and it worked. Just go in the text.txt and at a random place add a letter and it works. It's a weird error.