Oops, try again. Sorry, but I'm gonna need you to refresh the page! Don't worry your code is saved. I tried to refresh on my browser, but didn't work. Is there another refresh button?


#1




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

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


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


#2

this is a known bug:

i think appylpye nails it, his solution works really well


#3

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

This is my code which I have been written there like 1 week ago. It should work for you.


#4

so in essence the same solution as i linked to appylpye solution.


#5

Well basically yes I would say.


#6

:wink: but good solution, it would be better if the bug wasn't there at all, lets hope the new python course is better :slight_smile:


#7

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




#8

what is appylpye and how do I use it?


#9

it is a user (just like me and you), and i linked to his topic in which he explained the problem


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.