5/9 doesn't recognize "text.txt" as a legitimate file


#1

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

I'm not asking for help. I'm just letting whoever know that this only works if you try about a dozen different browsers, close and reopen, throw your laptop against the wall and come back an hour later and try it again.


Whats wrong with my code?
#2

I'm having this same problem right now. Have you gotten it to work?


#3

i skipped over it and then came back a bit later and it worked.


#4

The reason it does not work is because the file was deleted before you moved to that lesson, all you have to do in that event is just create a new file or using the try to generate a new file if it does not exist.

The below code prints out to console if it fails/succeeds and handles it in both events. Also depending on how you are implementing stuff you might need to add an IOError exception handle too.

MY CODE:

try:
    file = open("text.txt", "r+")
    print("File Opened")
except FileNotFoundError as error:
    print("You had an Error: %s" % error)
    print("Creating new text.txt file")
    file = open("text.txt", "w+")

#5

I had to put the following in front of my code:

my_list = [i**2 for i in range(1,11)]
f = open("text.txt", "w")
for item in my_list:
f.write(str(item) + "\n")
f.close()


#6

The same was happening with me so I rewrote the text.txt file myself before any other code and that worked fine.

f = open("text.txt", "w")

f.write("I'm the first line of the file!" + "\n")
f.write("I'm the second line." + "\n")
f.write("Third line here, boss." + "\n")

f.close()


#7

The error occrus because in the first time the file TEXT.TXT doen't exist. You need to create it.
The code is :

f = open("text.txt", "w")

f.write("I'm the first line of the file!" + "\n")
f.write("I'm the second line." + "\n")
f.write("Third line here, boss." + "\n")

f.close()

After this, you put :

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

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

my_file.close()

Whats wrong with my code?
#8

That is wonky... It worked by creating the file in the script. Maybe the codecademy peeps need to update the instructions to create text.txt instead of assuming the file exists.


#9

Click on the text.txt tab and change something inside file. For example: press "Enter" at the first line in the file. Then execute your code again. :smile:


#10

Here's my code:

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

Just make sure you use "r" instead of "r+" in this lesson.
Lastly, make a tiny change to the text.txt file. Add an extra period or anything! Here are the contents of my text.txt file:

I'm the first line of the file!
I'm the second line.
Third line here, boss...

Hope this helps!


#11

Just change something in the text.txt file.


#12

If you "cut" the text out of text.txt then "paste" it in again, it works.


#13

same problem here..and I had to re create the file

We can create and read lline by line like this:

f = open("text.txt","w")
f.write("I'm the first line of the file! \n I'm the second line.\n Third line here, boss.")
f.close()
my_file = open("text.txt","r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()


#14

Just open the "text.txt" edit something there and the code will work fine. :smile:


#15

Hi jorgerh Thanks for asking. I was in the same problem stopped and

Thanks to textpro07281 too , your code it helps me to finish tis course.!!!!


#16

why must we make a change to text.txt in order for it to work?


#17

It's probably a glitch in CodeCademy. I'm really not sure about the reason, I just know it works :laughing: Do you have an idea of why it happens?? It would be interesting to know. Thanks!

Make On!
RayvenMaker