Search notes:

Python: Iterate over each line in a file

The following simple Python script attempts to show how to iterate over each line of a (text) file:
with open('iter.txt') as txt_file:
     for txt_line in txt_file:
      #
      #  use end='' to omit printing new-lines
      # (The newline is already present in txt_line)
      #
         print (txt_line, end='')
Github repository about-python, path: /built-in/functions/open/iterate-over-each-line-in-file.py

See also

Skipping lines when reading a file
Using iter() and open() to iterate over each line in a file.
rstrip() might be used to remove the new line at the end of the read lines.

Index