5. Reading Between the Lines - It Worked, but now I get an error


#1



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

Alright, so this code worked and I was able to pass this part of the lesson. I went back to reference it and it didn't have the green bookmark by the lesson title. I hit Save & Sumbit Code and now I am getting this error:

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

And the console says;

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

I refreshed, rebooted, and tried the lesson on seperate computers and different browsers on each computer.

"text.txt" is defined and made available for me to use. It is made part of this lesson and is another tab.
If there is a work around of this, it will make me upset because I entered the code exatly how the lesson told me too. What is the issue?


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

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

my_file.close()


#2

@austinskyles,

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()

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

Thanks for the help! Is this an issue Codecademy knows about? Or is this an issue with internet browsers?


#4

Hey thanks for this. I was getting a bit annoyed with this one but then Im on a chrome book and figured it might be some issue with that. Anyway seeing all this first seemed a bit daunting and not the answer I was looking for but looking at it a bit longer its a great real world lesson to add to the course :wink: Thanks for sorting !


#5

4 posts were split to a new topic: 5. I cannot find my mistake


#6

Thanks this helped so much, I had no idea what was going wrong


#7

Thanks for your help! I can not handle it without your code.