Reading between the lines 5/9


#1


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

my_file=open("text.txt","r")
my_file.write("yossef\ngamal\nnagy")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()


#2

@noblegastoo,

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


Cannot found the text.txt on the codecademy's database
#3

@noblegastoo

Did you try opening the file in read and write: "r+"

I assume opening a file in read only mode ( "r" ) and then attempting write() would throw errors.

Also a handy trick just for the online environment:

cut-and-paste the contents of "text.txt" (right tab) back into text.txt to kind of 'refresh' the file. This stopped the "Sorry..." message for me

(thanks to...
pnewcom: who's comment is the only reason I thought to try this):
"solved it. I copied the sentences from the text.txt file and pasted it
back into the same file. After that python recognized the file again.
sorry for making a thread about this."


Cannot found the text.txt on the codecademy's database
#4

Worked for me too, thank you.


#5

Thank you. Your trick solved the issue.

But I wonder if Cademy put this intentionally?


#6

They wouldn't intentionally put a bug in their code. The real question is why are there so many bugs and why aren't they being fixed? I mean, this is just silly.


#7

This fixed the issue. I'm replying to keep the topic open so if other people get stuck on this they can find the answer.


#8

Thanks! The exercise seems slightly broken, as it refuses to recognize the existence of the text.txt file. Could be a database error?


#9

Had the same issue as the OP, although I was not trying to write to the file.

The solution from leonhard.wettengmx.n fixed it.
It also was very useful code to beginners in general.

Thank you


#10

what you have to do is press the "enter" at the 3'th line in the text.txt


#11

Thank you very, very much. This solved my problem.


#12

thank you! this needs to be marked as the solution!


#13

They are no longer supporting the Python program. That is why everything is still in Python 2. Any bugs you come across will most likely be ignored unless they decide to support Python again.


#14

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