- Before we move ahead to the portion
of this lesson on handling exceptions,
I just wanted to deal with a couple of
remaining open items for file processing.
First of all, we've only shown you the R and W
file open modes but there are several other
file open modes these are all of the ones
for text file processing and each one of these
also has a version with the letter V in it
so R-V, W-V, A-V, R-V+, W-V+, and A-V+
and each of those is for binary files
if in fact you have the need to process binary files
so you can do reading, writing and appending and
there's versions of each of those that
allow you to both read and write in the file as well
so you can pause the video and take a look at these
descriptions if you'd like to look at them in more detail.
Now separately, there's a couple of additional methods
that you may find useful if you're reading from a file,
there is a read method if it's a text file
it will read characters,
if it's a binary file it will read bytes
and the number of characters or bytes that you want to read
is specified as an integer argument to the method
or if you provide no arguments,
it will simply read the entire contents of the file.
There's also the read line method which returns one line
of text as a string and that will
include the new line character if there is one.
This particular method will return an empty string
if end file is encountered and if you're writing to a file,
a particularly handy method is the writelines method.
If you have a sequence of strings, writelines will output
the entire sequence to the file.
So you may recall when we first began
introducing file processing at the beginning of this lesson,
we called the write method many times over
to write one record at a time.
Well, if we simply took the strings that represented
each of those records and threw them into a list for example
we could have written them all out in one operation
by passing that list to the writelines method.
Now, as you may guess, there are classes that Python uses
to implement the file objects that we received
back from the open function each time
we opened a file for reading or for writing.
Those classes are defined in the
Python Standard Library module called I-O
and if you're interested in looking at the details
of those classes and all of the different methods
that they provide you can find that documentation here
on the Python dot org website.