Search code examples
pythonlistlineskip

Printing skips the first line


file1=open("filex.txt","r")
line=file1.readline()
for line in file1:
   print(line,end="")

So this prints out all the lines in the file, except the first line for some reason, help me out please!


Solution

  • Remove the line:

    line=file1.readline()
    

    which reads the first line and thus moves the file pointer past the first line before you start your actual line-by-line iteration. Generally, a file handle as returned by open(...) is an iterator that produces each line only once. You can, however, move it back to beginning by:

    file1.seek(0)