Refresh the page (Python)


#1

ok this is the last Python lesson on Number 5, i wrote the code and when i run it says (Oops, try again. Sorry, but I'm gonna need you to refresh the page! Don't worry your code is saved.) i refreshed the page like 50 times , i deleted the cache (and everything else) , i tried installing a new browser but it still doesnt fix the problem , it shows this error on the console (Traceback (most recent call last):
File "python", line 1, in
IOError: [Errno 2] No such file or directory: 'text.txt')

by the way here's the code
my_file = open("text.txt","r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()

Thanks :slightly_smiling:


#2

@arrayslayer46084,

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 man i was totally lost in this part