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

A file object is opened in line 22, with the w flag provided to indicate that the file is to be written to; the default mode when opening a file is read-only, as seen in line 25.

Line 23 writes a short text string to the file. When completed, Python 3 tells us how many characters were written to the file. Python 2.x doesn't automatically provide this feedback.

The file is manually closed in line 24, then reopened as a new filename in line 25. Because the default mode is read-only, we didn't have to provide the r flag.

The file is read in line 26, accepting data until the end of the line. The line read in is then printed to the screen. When we attempt to read the next line in the file (line 27), an empty string is returned to tell us there is nothing left.

If you want to put a new line in the file you're writing to (through the interactive shell), you must include the newline ( ) character every time you want to denote the end of a line; Python's write() method doesn't include it automatically. This is demonstrated in the following screenshot:

Writing and reading files

With line 28, we reopen the file using append mode; this allows us to write to the file without overwriting the data that is already present. Lines 29 and 30 add two additional lines to the file, then it is closed in line 31.

If we open the file to read it (lines 32 and 33), we see that all the lines added to the file are put into one string; there is no default separation between them, even though they were inserted separately.

So, we make a brand-new file in line 34. With lines 35 and 36, we write lines to the file but ensure that the newline character /n is added at the end of each line. The file is then closed and reopened.

If we send the data in the file to the readlines() method, we see a list of the two strings, both showing the newline characters. If we try to print more (line 40), we are presented with an empty string, as once the file is processed by one of the read methods, the data is purged from the variable.

If you want to view the file information as it would normally be seen, that is, outside of a list, you'll have to use the regular read() method, as shown in the following screenshot:

File read() method

Using the read() method reads the entire file contents, at once, into a string. The print() function then allows you to print a reader-friendly version of the file, one that doesn't show the newline characters.

If you want to save the buffered data to the file without closing out the file, you can use the flush() method instead of close(). As the name implies, the buffer is flushed out, sending the stored data to the actual file location while leaving the buffer open for more data. Thus, you can continue working with data without continuously opening and closing the file. For example, instead of using new_file.close() in line 31 in the previous screenshot, we could have used new_file.flush() to immediately write all data to the file, but new_file would remain open for more writing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.251.206