-2

I'm attempting to read and print a .txt file line-by-line in Python using the readline function. The below code is intended to print out the entire text file line-by-line, but as of right now, it only prints out the first five lines of the text file.

filename = input("Enter the name and extension of the file you want to open.")
file = open(filename, "r")
fileline = file.readline()
for line in fileline:
    fileline = fileline.rstrip("\n")
    print(fileline)
    fileline = file.readline()
file.close()

I expect the code to print out the entire file line by line, but it currently only prints out the first five lines. What is the bug in the code?

Sam Carpenter
  • 377
  • 3
  • 16
  • 3
    You should strive for a better title. Imagine a site in which every title was like yours, and what the front page would read like. – miradulo Feb 16 '17 at 22:34
  • 1
    How many characters are there in the first line of the file? – TigerhawkT3 Feb 16 '17 at 22:34
  • 2
    `for line in fileline` is looping through the characters of the line, not looping through lines. – Barmar Feb 16 '17 at 22:35
  • Why not try `for line in file: print(line)`? – jonrsharpe Feb 16 '17 at 22:37
  • @TigerhawkT3 Four on both- I could test it with different ones if you'd like! – Sam Carpenter Feb 16 '17 at 22:38
  • @jonrsharpe I tried it- it prints the individual digits of the first number on different lines, then stops. – Sam Carpenter Feb 16 '17 at 22:41
  • I think you mean four plus the newline character, totaling five. – TigerhawkT3 Feb 16 '17 at 22:42
  • @TigerhawkT3 I forgot about the newline! Thanks a billion! – Sam Carpenter Feb 16 '17 at 22:45
  • 1
    @SamRockitt I edited your post for brevity... you're gonna get a lot of negative points for putting personal language and so much repetitive verbiage in the post... feel free to edit further as you desire, but wanted to explain the reasoning behind the edits. Also I recommend you put the console output and file contents as you offered. Please put them as blocks of code, for readability. Thanks! – Jason R. Mick Feb 16 '17 at 22:50
  • 1
    @SamRockitt Also please post the exact wording of the assignment question for clarity. You keep alluding to it. Don't bother describing it in your own words... just post the exact question to cut down on the confusion. – Jason R. Mick Feb 16 '17 at 22:51
  • Apparent duplicate of http://stackoverflow.com/questions/1767513/read-first-n-lines-of-a-file-in-python ... the answer even is the exact same as one of the comments. – Jason R. Mick Feb 16 '17 at 23:06
  • Possible duplicate of [Read first N lines of a file in python](http://stackoverflow.com/questions/1767513/read-first-n-lines-of-a-file-in-python) – Jason R. Mick Feb 16 '17 at 23:07

1 Answers1

4

This line:

for line in fileline:

is looping through the characters of fileline, which contains the first line of the file. So if the first line has 5 characters, this loop will execute 5 times.

Then inside the loop, you print the line and then read the next line into the fileline variable. That has no effect on the loop, which is still iterating over the characters in the first line.

To make the program deliberately print the first 5 lines, you can do:

for i in range(5):
    fileline = file.readline()
    if (fileline == ''): #end of file reached
        break
    print(fileline.rtrim('\n'))

Or you can iterate over file, which automatically reads lines, and use a separate counter variable

i = 0
for line in file:
    print(line.rtrim('\n'))
    i += 1
    if i == 5:
        break
Barmar
  • 741,623
  • 53
  • 500
  • 612