Files and streams

Coming from a *nix background, Python treats a file as a data stream: each file is read and stored as a sequential flow of bytes. Each file has an end-of-file (EOF) marker denoting when the last byte of data has been read from it. Frequently, programs will read a file in pieces rather than loading the entire file into memory at one time. When the end-of-file marker is reached, the program knows there is nothing further to read and can continue with whatever processing it needs to do.

When a file is read, such as with a readline() method, the end of the file is shown at the command line with an empty string; empty lines are just strings with an end-of-line character. The following screenshot shows how this looks:

EOF example ...

Get Learn Programming in Python with Cody Jackson now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.