Exceptions and File Input

Slides:



Advertisements
Similar presentations
Perkovic, Chapter 7 Functions revisited Python Namespaces
Advertisements

Introduction to Computing Using Python Exceptions (Oops! When things go wrong)  Errors and Exceptions  Syntax errors  State (execution) errors.
Introduction to Computing Using Python Text Data, File I/O, and Exceptions  Strings, revisited  Formatted output  File Input/Output  Errors and Exceptions.
Introduction to Computing Using Python File I/O  File Input/Output  read(), readline(), readlines()  Writing to a file.
Lecture 07 – Exceptions.  Understand the flow of control that occurs with exceptions  try, except, finally  Use exceptions to handle unexpected runtime.
Exceptions COMPSCI 105 S Principles of Computer Science.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
General Programming Introduction to Computing Science and Programming I.
Exceptions 2 COMPSCI 105 S Principles of Computer Science.
Functions Reading/writing files Catching exceptions
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Errors And How to Handle Them. GIGO There is a saying in computer science: “Garbage in, garbage out.” Is this true, or is it just an excuse for bad programming?
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
Exceptions COMPSCI 105 SS 2015 Principles of Computer Science.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Introduction to Computing Using Python Exceptions (Oops! When things go wrong)  Errors and Exceptions  Syntax errors  State (execution) errors.
Lecture 4 Python Basics Part 3.
Python Built-in Exceptions Data Fusion Albert Esterline Source:
Lecture 07 – Exceptions.  Understand the flow of control that occurs with exceptions  try, except, finally  Use exceptions to handle unexpected runtime.
Python Exceptions and bug handling Peter Wad Sackett.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Python: Exception Handling Damian Gordon. Exception Handling When an error occurs in a program that causes the program to crash, we call that an “exception”
Exceptions in Python Error Handling.
Introduction to Computing Science and Programming I
CMPT 120 Topic: Python’s building blocks -> More Statements
Topic: File Input/Output (I/O)
George Mason University
Introduction to Python
File I/O File input/output Iterate through a file using for
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
Lecture 4 Python Basics Part 3.
Topics Introduction to Repetition Structures
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Why exception handling in C++?
Exceptions and files Taken from notes by Dr. Neil Moore
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Exception Handling Chapter 9.
Python Primer 2: Functions and Control Flow
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Exception Handling.
Python’s Errors and Exceptions
File I/O File input/output Iterate through a file using for
Exceptions and files Taken from notes by Dr. Neil Moore
Lecture 4 Python Basics Part 3.
Exception Handling Chapter 9 Edited by JJ.
File I/O File input/output Iterate through a file using for
ERRORS AND EXCEPTIONS Errors:
(Oops! When things go wrong)
Exceptions.
Introduction to Python: Day Three
CS190/295 Programming in Python for Life Sciences: Lecture 3
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Python Syntax Errors and Exceptions
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
By Ryan Christen Errors and Exceptions.
Topics Introduction to File Input and Output
Python Exceptions and bug handling
Class code for pythonroom.com cchsp2cs
Dealing with Runtime Errors
Presentation transcript:

Exceptions and File Input A201 – Dr. Wennstrom

Types of errors There are basically two types of errors: syntax errors Tools for Computing: Python There are basically two types of errors: syntax errors erroneous state errors >>> excuse = 'I'm sick' SyntaxError: invalid syntax >>> print(hour + ':' + minute + ':' + second) TypeError: unsupported operand type(s) for +: 'int' and 'str’ >>> infile = open('foo.txt') IOError: [Errno 2] No such file or directory: 'foo.txt’

Syntax errors Tools for Computing: Python Syntax errors are due to the incorrect format of a statement Detected when the statement is translated to machine language, before it is executed. >>> (3+4] SyntaxError: invalid syntax >>> if x == 5 >>> print 'hello' >>> ls = [4;5;6] >>> for i in range(10): print(i) SyntaxError: expected an indented block

Erroneous state errors Tools for Computing: Python The program execution gets into an erroneous state during run-time >>> 3 / 0 ZeroDivisionError: division by zero >>> ls NameError: name 'ls' is not defined >>> ls = [12, 13, 14] >>> ls[3] IndexError: list index out of range >>> ls = ls * ls TypeError: can't multiply sequence by non-int of type 'list' >>> int('4.5') ValueError: invalid literal for int() with base 10: '4.5'

Erroneous state errors Tools for Computing: Python The program execution gets into an erroneous state during run-time When a run-time error occurs, an “error” object is created This object has a type that is related to the type of error The object contains information about the error The default behavior is to print this information and interrupt the execution of the statement. When a run-time error occurs, an “error” object is created This object has a type that is related to the type of error The object contains information about the error The “error” object is called an exception; the creation of an exception due to an error is called the raising of an exception The programmer can do something special*, using ways to catch an exception and redirect it so as to override the default behavior elsewhere for handling. *We will see how to do this in a little bit.

Raising an exception Tools for Computing: Python >>> while True: pass Traceback (most recent call last): File "<pyshell#53>", line 2, in <module> KeyboardInterrupt >>> raise ValueError() File "<pyshell#54>", line 1, in <module> raise ValueError() ValueError >>> raise ValueError('Just joking...') File "<pyshell#55>", line 1, in <module> raise ValueError('Just joking...') ValueError: Just joking... >>> >>> while True: pass Traceback (most recent call last): File "<pyshell#53>", line 2, in <module> KeyboardInterrupt >>> raise ValueError() File "<pyshell#54>", line 1, in <module> raise ValueError() ValueError >>> raise ValueError('Just joking...') File "<pyshell#55>", line 1, in <module> raise ValueError('Just joking...') ValueError: Just joking... >>> try: except: print('Caught exception.') Caught exception. >>> >>> while True: pass Traceback (most recent call last): File "<pyshell#53>", line 2, in <module> KeyboardInterrupt >>> raise ValueError() File "<pyshell#54>", line 1, in <module> raise ValueError() ValueError >>> >>> while True: pass >>> while True: pass Traceback (most recent call last): File "<pyshell#53>", line 2, in <module> KeyboardInterrupt >>> By typing Ctrl-C, a user can force a KeyboardInterrupt exception to be raised Any exception can be raised within a program with the raise statement ValueError, like all exception types, is a class ValueError() uses the default constructor to create an exception (object) statement raise switches control flow from normal to exceptional The constructor can take a “message” argument to be stored in the exception object

Exception types Tools for Computing: Python Technically an exception is a group of types, just like sequence can be strings, tuples, and lists. Some of the built-in exception classes: Exception Explanation KeyboardInterrupt Raised when user hits Ctrl-C, the interrupt key OverflowError Raised when a floating-point expression evaluates to a value that is too large ZeroDivisionError Raised when attempting to divide by 0 IOError Raised when an I/O operation fails for an I/O-related reason IndexError Raised when a sequence index is outside the range of valid indexes NameError Raised when attempting to evaluate an unassigned identifier (name) TypeError Raised when an operation of function is applied to an object of the wrong type ValueError Raised when operation or function has an argument of the right type but incorrect value

Exceptions Tools for Computing: Python When the program execution gets into an erroneous state, an exception object is created This object has a type that corresponds to the type of error The object contains information about the error The default behavior is to print this information and "crash" "Exception" does not mean error. It means that the normal execution flow of the program is interrupted and execution switches to the exceptional execution flow.

Exceptions Tools for Computing: Python The precise categorization of what counts as a ”runtime error" or just an "exception", is different in different programming languages. We're not going to worry to much about those distinctions in this class. But you do need to know what kinds of situations will cause which type of exception in order to deal with them. To talk about how we will deal with these exceptions without crashing, let's revisit a problem similar to ones we've had in the past. In fact, that will be our quiz for today.

Do this: print("Please enter a number:") user_num = float(input()) Tools for Computing: Python print("Please enter a number:") user_num = float(input()) print("The square of " + str(user_num) + " is " + str(user_num**2) + ".") If we wanted to verify the user's input first using if, we'd need to somehow predict ahead of time whether or not the user's text will successfully convert into a float. Such as….? The string method .isnumeric() only checks if the numbers are all digits (not decimals or fractions, negative numbers, etc.) You could check all the possibilities ahead of time (.isdigit(), .isdecimal(),…), but that could get complicated. But Python already knows how to convert strings into floats. It'd be nice if we could just ask the interpreter to try to convert the string into a float, and have it just alert us (instead of crashing) if something goes wrong. floatingPoint.py

Catching and handling exceptions Tools for Computing: Python We can override the default handling of a raised exception by using a try/except statement If an exception is raised during a try block, then the block of the associated except is executed (ie., our contingency plan) try: age = int(input('Enter your age: ')) print('You're nearly {} years old.'.format(age + 1)) except: print('Enter your age using digits 0-9!') age = int(input('Enter your age: ')) print('You're nearly {} years old.'.format(age)) The except code block is the exception handler Custom behavior: Default behavior: >>> ========== RESTART ========== >>> Enter your age: fifteen Enter your age using digits 0-9! >>> ======================== RESTART ======================== >>> Enter your age: fifteen ValueError: invalid literal for int() with base 10: 'fifteen'

Format of a try/except statement Tools for Computing: Python The format of a try/except is: The exception handler processes any exception raised in the try block The except statement is said to catch the (raised) exception try: <indented code block> except: <exception handler block> <non-indented statement> We can parameterize the except clause to only catch exceptions of a specific type try: <indented code block> except <ExceptionType>: <exception handler block> <non-indented statement>

Format of a try/except statement Tools for Computing: Python def read_age(filename): 'converts first line of file filename to an integer and prints it' try: infile = open(filename) age = int(infile.readline()) print('age:', age) except ValueError: print('Value cannot be converted to integer.') We can parameterize the except clause to only catch exceptions of a specific type 1 fifteen >>> read_age('age.txt') Value cannot be converted to integer. >>> read_age('age.txt') Value cannot be converted to integer. >>> read_age('age.text') IOError: No such file or directory: 'age.text' age.txt default exception handler prints this

Multiple exception handlers Tools for Computing: Python def read_age(filename): 'converts first line of file filename to an integer and prints it' try: infile = open(filename) age = int(infile.readline()) print('age:', age) except IOError: # executed only if an IOError exception is raised print('Input/Output error.') except ValueError: # executed only if a ValueError exception is raised print('Value cannot be converted to integer.') except: # executed if an exception other than IOError or ValueError is raised print('Other error.') It is possible to restrict the except statement to catch exceptions of a specific type only

Now do this: print("Please enter a number:") Tools for Computing: Python print("Please enter a number:") try: user_num = float(input()) print("The square of " + str(user_num) + " is " + str(user_num**2) + ".") except ValueError: print("That wasn't a valid number.") tryExcept.py

What we really want is to do this: Tools for Computing: Python while True: print("Please enter a number:") try: user_input = float(input()) print("The square of " + str(user_input) + " is " \ + str(user_input**2) + ".") break except ValueError: print("That wasn't a valid number. Please try again.") tryExceptWhileTrue.py

Exceptions and Files

What we really want is to do this: Tools for Computing: Python There's a whole category of errors that can crop up when working with files. FileNotFoundError if you try to open a file using a filename that doesn't exist. IsADirectoryError if you try to open a directory and read it as if it were a text file. PermissionError if you try to open a file that the current user doesn't have permissions to access.  Fortunately, all of these error types related to file input and output are subtypes of IOError, so we can account for any kind of problem involving working with files by just catching the IOErrors. tryExceptWhileTrue.py

Files and the file system Tools for Computing: Python root folder / Applications Users bin var Firefox.app Mail.app Shared cepope Contents MacOS Info Info / Applications Users bin var Firefox.app Mail.app Shared cepope Contents MacOS poem.txt image.jpg Mail The file system is the OS component that organizes files and provides a way to create, access, and modify files poem.txt Files are organized in a tree structure folders (or directories) folders (or directories) regular files binary file text file The pathname uniquely identifies the file Absolute pathnames /var/poem.txt /Users/cepope/poem.txt /Applications/Mail.app/ Relative pathnames (relative to current working directory Users) cepope/poem.txt ./cepope/image.jpg ../var/poem.txt

Current working directory Tools for Computing: Python >>> import os >>> os.getcwd() '/Users' >>> os.chdir('/Users/cepope') '/Users/cepope' >>> import os >>> os.getcwd() '/Users' >>> os.chdir('/Users/cepope') >>> import os >>> os.getcwd() '/Users' >>> import os

Opening and closing a file Tools for Computing: Python Processing a file consists of: Opening the file Reading from and/or writing to the file Closing the file Built-in function open() is used to open a file The second (optional) argument is the file mode Returns a “file” object The first input argument is the file pathname — may be absolute or relative to cwd (current working directory) File mode 'r' is used to open a file for reading A “file” object is of a type that supports several “file” methods, including close() >>> infile = open('foo.txt') IOError: No such file or directory: 'foo.txt' >>> infile = open('poem.txt', 'r') >>> infile.close() >>> infile = open('foo.txt') IOError: No such file or directory: 'foo.txt' >>> infile = open('foo.txt') IOError: No such file or directory: 'foo.txt' >>> infile = open('poem.txt', 'r')

From now on: Tools for Computing: Python every command that works with files must be inside of a try block so that you can catch any IOErrors. Open fileprinter.py from Lab#8 print("Please input a filename.") filename = input("> ") file = open(filename) contents = file.read() print(contents) file.close() Fileprinter.py

Do this: Open fileprinter.py from Lab#8 Tools for Computing: Python Open fileprinter.py from Lab#8 print("Please input a filename.") filename = input("> ") file = open(filename) contents = file.read() print(contents) file.close() fileprinter.py

Improve using try-except: Tools for Computing: Python while True: print("Please input a filename.") filename = input("> ") try: file = open(filename) contents = file.read() file.close() break except IOError: print("There was an error opening that file.") print(contents) There is one problem with this code. If an error happens after the file is opened but before it is closed, then the file never gets closed! fileprinter.py

with and as: Tools for Computing: Python Instead of explicitly closing files using .close(), Python 3 has the keywords with and as which will automatically handle the process of making sure that files are closed for you. Here’s the syntax: with command-to-create-file as variable-to-store-file: #code that works with the file object From now on, you should always use with-as when working with files fileprinter.py

Fix up our fileprinter… Tools for Computing: Python while True: print("Please input a filename.") filename = input("> ") try: with open(filename) as file: contents = file.read() break except IOError: print("There was an error opening that file.") print(contents) fileprinter_try.py

Moving Through a File

Open file mode The file mode defines how the file will be accessed Tools for Computing: Python The file mode defines how the file will be accessed Mode Description r Reading (default) w Writing (if file exists, content is wiped) a Append (if file exists, writes are appended) r+ Reading and Writing t Text (default) b Binary >>> infile = open('poem.txt') >>> infile = open('poem.txt', 'r') >>> infile = open('poem.txt', 't') >>> infile = open('poem.txt', 'rt') These are all equivalent

File methods Tools for Computing: Python There are several “file” types; they all support similar “file” methods Methods read() and readline() return the characters read as a string Methods readlines() returns the characters read as a list of lines Method write() returns the number of characters written Usage Description infile.read(n) Read n characters starting from cursor; if fewer than n characters remain, read until the end of file infile.read() Read starting from cursor up to the end of the file infile.readline() Read starting from cursor up to, and including, the end of line character infile.readlines() Read starting from cursor up to the end of the file and return list of lines outfile.write(s) Write string s to file outfile starting from cursor infile.close(n) Close file infile

Reading a file Tools for Computing: Python 1 Once upon a midnight dreary, while I pondered weak and weary,\n 2 Over many a quaint and curious volume of forgotten lore,\n 3 While I nodded, nearly napping, suddenly there came a tapping\n ⌃ poem.txt When the file is opened, a cursor is associated with the opened file The initial position of the cursor is: under the first character of the file, if file mode is r beyond the end of the file, if file mode is a or w >>> infile = open('poem.txt') >>> infile.read(1) 'O' >>> infile = open('poem.txt') >>> infile.read(1) 'O' >>> infile.read(20) 'nce upon a midnight ' >>> infile.readline() 'dreary, while I pondered weak and weary,\n' >>> infile = open('poem.txt') >>> infile.read(1) 'O' >>> infile.read(20) 'nce upon a midnight ' >>> infile.readline() 'dreary, while I pondered weak and weary,\n' >>> s = infile.read() >>> len(s) 6238 >>> infile = open('poem.txt') >>> infile.read(1) 'O' >>> infile.read(20) 'nce upon a midnight ' >>> infile.readline() 'dreary, while I pondered weak and weary,\n' >>> s = infile.read() >>> len(s) 6238 >>> infile.close() >>> infile = open('poem.txt') >>> infile = open('poem.txt') >>> infile.read(1) 'O' >>> infile.read(20) 'nce upon a midnight '

Patterns for reading a text file Tools for Computing: Python Common patterns for reading a file: Read the file content into a string Read the file content into a list of words Read the file content into a list of lines Example: def word_count(filename): 'returns the number of words in file filename' infile = open(filename) content = infile.read() infile.close() words = content.split() return len(words) def char_count(filename): 'returns the number of characters in file filename' infile = open(filename, 'r') content = infile.read() infile.close() return len(content) def line_count(filename): 'returns the number of lines in file filename' infile = open(filename) lines = infile.readlines() infile.close() return len(lines)

More file methods Example: Tools for Computing: Python You can use the method .tell() to figure out exactly where the pointer is, and you can move the pointer to a specific location using .seek(). Example: >>> file.read() "never hope to see one.\nBut I can tell you anyhow,\nI'd rather see than be one.\n" >>> file.read(3) ' ' >>> file.seek(5) 5 >>> file.tell() >>> file.read(7) 'e never' 12 >>> file.seek(0)

Quiz Tools for Computing: Python Let's redo our fileprinter.py script so that it reads and prints lines from the file one at a time Solution: while True: print("Please input a filename.") filename = input("> ") try: with open(filename) as file: line = file.readline() if line == "": break else: print(line) except IOError: print("There was an error opening that file.")