defaults to r (input), and the buffering policy is to enable full buffering. For special
needs, here are a few things you should know about these three open arguments:
Filename
As mentioned earlier, filenames can include an explicit directory path to refer to
files in arbitrary places on your computer; if they do not, they are taken to be names
relative to the current working directory (described in the prior chapter). In general,
most filename forms you can type in your system shell will work in an open call.
For instance, a relative filename argument r'..\temp\spam.txt' on Windows
means spam.txt in the temp subdirectory of the current working directory’s
parent—up one, and down to directory temp.
Open mode
The open function accepts other modes, too, some of which we’ll see at work later
in this chapter: r+, w+, and a+ to open for reads and writes, and any mode string
with a b to designate binary mode. For instance, mode r+ means both reads and
writes are allowed on an existing file; w+ allows reads and writes but creates the file
anew, erasing any prior content; rb and wb read and write data in binary mode
without any translations; and wb+ and r+b both combine binary mode and input
plus output. In general, the mode string defaults to r for read but can be w for write
and a for append, and you may add a + for update, as well as a b or t for binary or
text mode; order is largely irrelevant.
As we’ll see later in this chapter, the + modes are often used in conjunction with
the file object’s seek method to achieve random read/write access. Regardless of
mode, file contents are always strings in Python programs—read methods return
a string, and we pass a string to write methods. As also described later, though, the
mode string implies which type of string is used: str for text mode or bytes and
other byte string types for binary mode.
Buffering policy
The open call also takes an optional third buffering policy argument which lets you
control buffering for the file—the way that data is queued up before being trans-
ferred, to boost performance. If passed, 0 means file operations are unbuffered
(data is transferred immediately, but allowed in binary modes only), 1 means they
are line buffered, and any other positive value means to use a full buffering (which
is the default, if no buffering argument is passed).
As usual, Python’s library manual and reference texts have the full story on additional
open arguments beyond these three. For instance, the open call supports additional
arguments related to the end-of-line mapping behavior and the automatic Unicode
encoding of content performed for text-mode files. Since we’ll discuss both of these
concepts in the next section, let’s move ahead.
File Tools | 145