5 reading between the lines - output not displaying correctly


got a wierd issue so i guessed that the argument that is passed to the readline function is the line number so wrote a while loop to iterate through the lines but it doesn't print the lines. could someone explain why i am just getting first and last few characters? is the ' in I'm confusing it somehow?

BTW if you are having issues with theno such file exists with your text.txt then go to the tab add a character and delete it and then go back to your script and run it

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

while len(my_file.readline(i))!=0:
    print my_file.readline(i)

what i think it should output:
I'm the first line of the file!
I'm the second line.
Third line here, boss.

what is getting output:
ne o
** file**
I'm th
d line.
Third li

i think the argument might be number of characters


have got past this now but don't understand why the line is incremented every time the function is called or how you control which line is called


readline() holds its pointer position in the file. A line ends with '\n' so that's where the pointer sits. If a subsequent readline() directive is given, the line at the pointer gets written and the pointer advances to the end of that line.

When we give it an argument, well, you've seen already how that works. The pointer stays in the line where it is stopped in its tracks. Since the print statement in your code does not end with a comma, Python inserts a '\n'.


so there isn't a way to manipulate the pointer?
just goes sequentially through from the start?
surely there must be a way to control the line output?

sorry hadn't noticed this reply before have been going through the other courses and haven't had to use the forum until i got to java script :slightly_smiling: really loving all these great courses