Opening Files and File Objects - Page 9
May 24, 2001
Probably the single most common thing you will want to do with
files is open and read them. In Python, opening and reading a
file is accomplished using the built-in open
function, and various different built-in reading operations. This
following short Python program reads in one line from a text file
named "myfile":
fileobject = open("myfile", 'r')
line = fileobject.readline()
open doesn't actually read anything from the file;
instead it returns an abstract object called a file
object, representing the opened file. All Python file I/O is
done using file objects, rather than file names.
Use open to open a file for reading
or writing. open returns a file
object.
The first call to readline returns the first line in
the file object, everything up to and including the first newline
character, or all of the file if there is no newline character in
the file; the next call to readline would return the
second line, and so on. So, a file object is something that keeps
track of a file and how much of the file has been read, or
written. The first argument to the open function is a pathname.
In the above example we are opening what we expect to be an
already existing file in the current working directory. The
following would open a file at the given absolute location:
import os
fileName = os.path. join("c:", "My Documents", "test", "myfile")
fileobject = open(fileName, 'r')
Closing Files
Use close to close a file object once you are
done with it.
|
|
Once all data has been read from or written to a file object, it
should be closed. Closing a file object frees up system
resources, allows the underlying file to be read or written to by
other code, and, in general, makes the program more reliable. In
small scripts, not closing a file object will generally not have
much of an effect; file objects are automatically closed when the
script or program terminates. In larger programs, too many open
file objects may exhaust system resources, causing the program to
abort.
Closing file objects is done using the close method,
after the file object is no longer needed. The short program
above then becomes this:
fileobject = open("myfile", 'r')
line = fileobject.readline()
# . . . any further reading on the fileobject . . .
fileobject.close()
Opening Files in Write or Other Modes
The second argument of the open command is a single character
denoting how the file should be opened. 'r' means
open the file for reading: 'w' means open the file
for writ-ing (any data already in the file will be erased): and
'a' means open the file for writing (new data will
be appended to the end of any data already in the file). If you
want to open the file for reading, you can leave out the second
argument. 'r' is the default. The follow-ing short
program writes "Hello, World" to a file:
fileobject = open("myfile", 'w')
fileobject.write("Hello, World\n")
fileobject.close()
Depending on the operating system, open may also
have access to additional file modes. In general, these are not
necessary for most purposes. As you write more advanced Python
programs, you may wish to consult the Python reference manuals
for details.
open will open a file for reading,
(over) writing, or appending, depending on the value if its
second argument, which can be 'r', 'w', or
'a'.
As well, open can take an optional third argument,
which defines how reads or writes for
that file are buffered. Buffering is the process of
holding data in memory until enough has been requested or written
to justify the time cost of doing a disk access. Again, this is
not something you typically need to worry about, but as you
become more advanced in your use of Python, you may wish to read
up on it.
The Built-In Namespace - Page 8
The Quick Python Book
Functions to Read and Write Text or Binary Data - Page 10
|