Cannot complete lesson. File not found error


#1



Hi Everyone,

I cant seem to finish Python Lesson 12/05 File Input/Output: Reading Between the Lines.

https://www.codecademy.com/courses/python-intermediate-en-OGNHh/1/1?curriculum_id=4f89dab3d788890003000096

When I run my code I get the following error in the console:

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

There is an additional error shown on the bottom of the screen:

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

After I reload the page, the same errors pop up.

Here is my code, but i don't think anything should be throwing the error. Any ideas for help?

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

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

my_file.close()


#2

@moderateurs I think that @digitalace69972 is right, and that this is a bug, but forget how to report one.


#3

@digitalace69972,

With
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....
and will create the text.txt

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)
#my_file.close()

#4

@digitalace69972,

import os
print "== if the file doesn't exist, create one =="
if os.path.exists('text.txt'):
    print "File exists"
else:
    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.close()

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

Reference:
== discussions / opinions ==
http://stackoverflow.com/questions/11968976/list-files-in-only-the-current-directory
http://stackoverflow.com/questions/18533621/creating-a-new-text-file-with-python
http://stackoverflow.com/questions/5214578/python-print-string-to-text-file


3/9 Writing
#5

Hi Leonhard,

Thanks so much! Your code worked! Output below..

== if the file doesn't exist, create one ==
Creating the text.txt-file
I'm the first line of the file!

I'm the second line.

Third line here, boss.

None


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

@leonhard.wettengmx.n This, while true, doesn't negate the fact that there is a problem with the exercise. Do you know how to report such a problem?

Am I wrong @mtf?


#7

This is a well documented issue with several work arounds in the forums. We have no way to fix it, so must work with what we have.


#8

Ok, thanks, I had no idea. @leonhard.wettengmx.n @mtf


#9

This worked for me, thanks!