Reading between the lines 5/9


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

<What do you expect to happen instead?>

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:

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

1 Like


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)


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"
    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 = open("text.txt","r")
print my_file.readline()
print my_file.readline()
print my_file.readline()

== discussions / opinions ==



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


Worked for me too, thank you.

Thank you. Your trick solved the issue.

But I wonder if Cademy put this intentionally?

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.

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.

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

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

what you have to do is press the “enter” at the 3’th line in the text.txt

1 Like

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

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

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.

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