File Manipulation - Possible Bug


Hey everyone,

I have happily completed every python lesson the codecademy has to offer, except for a single exercise. This is under the File Input/Output unit. Click here to see the lesson.

The program, though correct in python, as tested in IDLE, has an error in codecademy. This may be a bug. It sends the following error:

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

Does anyone have any way to get around that error? Please let me know. Thanks.


The test done

import os.path
if not os.path.isfile("text.txt"):
    print("Sorry, but I'm gonna need you to refresh the page! Don't worry your code is saved.")

Thus if the file text.txt does not exist you will get the message....

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

you are trying to open an existing 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)


With following code
you will do a test of the existence of text.txt file
and if the file text.txt is not found
you will create the text.txt file

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 ==


Thanks @leonhard.wettengmx.n

That fixed the problem and completed python for me.