5. Reading Between the lines



it prints this out:

This should be printed out:
I'm the first line of the file!
I'm the second line.
Third line here, boss.

my_file = open("text.txt", 'r')
print my_file.readline(1)
print my_file.readline(2)
print my_file.readline(3)


That's because you have numbers in your print my_file.readline()s. In order to read the whole line, you don't need the numbers.


Is it my mistake, or is it a glitch?
Browser reboot doesnt help.


Same problem here! So far I didn't find a solution.

executing the following code should give the files (and folders) in the current working directory. In my case it only shows the file 'script.py' and because 'text.txt' is missing, I think it cannot be opened with open().

import os

For the excercise maybe (if possible) try to create such a file first and then opening it and using readline().

We're not the first ones, see here (with "solution"):


Yeah, it works. Thanks!


I am also getting the same issue!


Thank you, it works)


use this code, I have done it...
print my_file.readline()
print my_file.readline()
print my_file.readline()


More easy... go to "output.txt" tab, press on last line and them click in "Save & Submit Code". This will write this file on disk and let you finish the exercise.


Though when you go to text.txt file it is showing some content, but in the database the file is empty. So you need to write some line in the text.txt file and the read it out!
I tried the code below and it worked!

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


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