Python 101 : Reading text from a file


The below example reads input from the "/home/albert/file" file and prints it:


We use "StopIteration" exception to exit a loop so it doesn't go forever. 

We use the next() function to iterate through the content of the file.
 you can also instruct it to return a terminating value like "None" for example.

The print() function uses the "end" parameter - parameter with which the printed output ends -, the default value is "\n".

Comments

Leave as a comment:

Archive