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
   134   135   136   137   138   139   140   141   142   143   144