Search notes:

Python: using iter() and open()

The following simple example tries to demonstrate how the built-in functions iter() and open() might be used to iterate over each line of a file:
#!/usr/bin/python3

with open('iter.txt') as txt:
     for line in iter(txt.readline, ''):
       # Remove trailing new line because…
         line = line.rstrip('\n')
       # … it is implicitely added in print()
         print(line)
Github repository about-python, path: /built-in/functions/open/iter.py

See also

This page might have a more natural way to iterate over lines in a file.

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...', 1758198439, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/built-in-functions/open/iter(48): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78