It seems it's a bug of code cademy site, Am I right?


#1



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


Hello, This is my code for lesson 5:
my_file=open("text.txt", "r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()

And this is the error that consul shows me:
Traceback (most recent call last):
File "python", line 1, in
IOError: [Errno 2] No such file or directory: 'text.txt'
And this is what code cademy says:

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

And I refreshed the page many times but it didn't work.
What is going on here? and what's the problem?


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


#2

@anahita.hf,
There seem to be 2 soultions...

-1

You have to =click= on text.txt tab once, therewith getting ownership of this file

OR

-2

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

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

==================================================

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

I got that bug too.

I simply override their file with the same content and it got fixed.


#4

This worked for me. If you simply cut/paste the content of the file everything runs smoothly.


#5

Thank you for helping me, It's working with your code. :relaxed:


#6

Thank you for answering, yes I fix the problem thanks to leonhard.wettengmx.n.
But anyway thank you to sharing your experience of this problem. :blush:


#7

Thank you too for sharing your solution with me. :cherry_blossom::green_heart:


#8

Yes, I got the same result. Not sure what's happening though. I'm hoping it's just a problem on the Codecademy site and not something I'm failing to understand.


#9

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