Page 139 -
P. 139
files and exceptions
Your Python Toolbox
You’ve got Chapter 3 under your
belt and you’ve added some key
Python techiques to your toolbox. Use the open() BIF to open a disk file,
creating an iterator that reads data from CHAPTER 3
the file one line at a time.
The readline() method reads a
Python Lingo The seek() method can be used to
single line from an opened file.
• An “exception” occurs as a result “rewind” a file to the beginning.
of a runtime error, producing a
The close() method closes a
traceback. previously opened file.
• A “traceback” is a detailed string into a list of parts.
The split() method can break a
description of the runtime error
that has occurred. An unchangeable, constant list in Python
is called a tuple. Once list data is
assigned to a tuple, it cannot be changed.
Tuples are immutable.
IDLE Notes A ValueError occurs when your data
does not conform to an expected format.
• Access Python’s documentation
An IOError occurs when your data
by choosing Python Docs from
cannot be accessed properly (e.g.,
perhaps your data file has been moved or
IDLE’s Help menu. The Python 3
renamed).
documentation set should open in
your favorite web browser.
The help() BIF provides access to
Python’s documentation within the IDLE
shell.
The find() method locates a specific
substring within another string.
The not keyword negates a condition.
The try/except statement provides
an exception-handling mechanism,
allowing you to protect lines of code that
might result in a runtime error.
The pass statement is Python’s empty
or null statement; it does nothing.
you are here 4 103