Python readline() only working for me if I use it more than once?!

So I have built a replit with a text file containing a few random Shakespeare quotes.

When I write the code

myfile = open("shakespeare.txt","r")
print(myfile.readline())
print(myfile.readline())
myfile.close()

The first two lines from the text file are displayed

But when I delete the second print(myfile.readline()) nothing is displayed.

Can anyone explain what is going on / why the first line of the text file is NOT displayed? Thanks is advance!

Repl link:

Hey @rbrownnhl!

Could you try adjusting your zoom level to see if the output shows up?

Interesting !
When I initially run the code with two sets of print(myfile.readline()) the first two lines of the text file are displayed when the code runs - but when I change the zoom, the first line of text disappears and only the second line of text remains displayed…

Does this help diagnose what my problem is?

That does confirm that your issue is just another chunk of the weird issues relating to specific zooms right now, could you see if you can find a zoom where the single line shows up?

1 Like

u should try using

with open("shakespear.txt", "r+") as f:
       myfile = f.readline()

print(myfile)

That’s not at all what they wanted, and that would only print the last line from the file?

With a single instance of print(myfile.readline()) when I change the zoom, I get what I was expecting (i.e. the first line of the text file) for an instant (much less than a second) then it disappears and nothing is displayed.

With two instances of print(myfile.readline()) at some zooms I get both the first and second lines of the text file displayed, and at other zooms I just get the second line of the text file displayed.

So… my code is fine, and there is something up with my browser (Chrome) and/or replit??

This is the case, yes. It’s a fairly recent issue, and Replit Staff are aware of it.

2 Likes