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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1745619659, '3.142.92.19', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/built-in-functions/open/iterate-over-each-line-in-file(53): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78