Taken from notes by Dr. Neil Moore & Dr. Debby Keen

Slides:



Advertisements
Similar presentations
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Advertisements

11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
Files in Python The Basics. Why use Files? Very small amounts of data – just hardcode them into the program A few pieces of data – ask the user to input.
Files in Python Input techniques. Input from a file The type of data you will get from a file is always string or a list of strings. There are two ways.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
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.
Guide to Programming with Python Chapter Seven (Part 1) Files and Exceptions: The Trivia Challenge Game.
CS190/295 Programming in Python for Life Sciences: Lecture 3 Instructor: Xiaohui Xie University of California, Irvine.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Files Tutor: You will need ….
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
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])
Lecture 4 Python Basics Part 3.
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.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
Exceptions in Python Error Handling.
Introduction to Computing Science and Programming I
Topic: File Input/Output (I/O)
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.
CS 115 Lecture 8 Structured Programming; for loops
Creating and Modifying Text part 2
Exceptions and files Taken from notes by Dr. Neil Moore
Chapter 14: Exception Handling
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Files and Streams Lect3 CT1411.
Exceptions 10-Nov-18.
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Ruth Anderson UW CSE 160 Winter 2017
Ruth Anderson UW CSE 160 Spring 2018
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
File IO and Strings CIS 40 – Introduction to Programming in Python
Using files Taken from notes by Dr. Neil Moore
Exceptions and files Taken from notes by Dr. Neil Moore
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Fundamentals of Data Structures
Fundamentals of Data Representation
ERRORS AND EXCEPTIONS Errors:
(Oops! When things go wrong)
Exceptions.
CS 1111 Introduction to Programming Fall 2018
Exceptions 19-Feb-19.
Machine Architecture and Number Systems
Machine Architecture and Number Systems
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Recursion Taken from notes by Dr. Neil Moore
CISC101 Reminders All assignments are now posted.
For loops Taken from notes by Dr. Neil Moore
Topics Introduction to File Input and Output
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
Exceptions 25-Apr-19.
Winter 2019 CISC101 4/29/2019 CISC101 Reminders
Exceptions 22-Apr-19.
Exceptions 10-May-19.
Topics Introduction to File Input and Output
Exceptions 5-Jul-19.
CMSC 202 Exceptions.
CS 1111 Introduction to Programming Spring 2019
Presentation transcript:

Taken from notes by Dr. Neil Moore & Dr. Debby Keen CS 115 Lecture 18 Exceptions and files Taken from notes by Dr. Neil Moore & Dr. Debby Keen

Run-time errors Remember the three types of errors: Syntax error (code will not run) Run-time error (detected when code runs, crashes) Semantic error (not detected by interpreter, program gives incorrect output or behavior) Another name for a run-time error in Python is an exception. Probably from “the exception, not the rule” Signaling a run-time error is called raising an exception Also called “throwing” an exception (C++ and Java)

Exceptions By default, raising an exception crashes your program. But exceptions can be caught and handled. There are many different kinds of exceptions: TypeError: argument has the wrong type ValueError: argument has a good type but a bad value IndexError: accessing a sequence (a string, a list) out of range ZeroDivisionError: just what it says IOError: file problem, such as “file not found” RuntimeError: “none of the above” https://docs.python.org/3/library/exceptions.html

Catching exceptions By default, exceptions cause the program to crash. Because that’s better than continuing to run and doing the wrong thing But sometimes you might have a better idea for how to handle it For example, type-casting a string to int if the string wasn’t numeric, Python can’t give you a number You asked Python to do something and it can’t Exception! But maybe you know what to do in this particular case If it was user input, repeat the input and ask again If it came from a file, maybe ignore that line Maybe the program can use a default value Sometimes you can prevent the exception by checking before you do the action, like comparing a denominator to zero before dividing by it But there are errors that you cannot check for without trying it

try/except syntax To catch an exception, you use a try / except statement: try: body that might raise an exception except ExceptionClass: handle the exception code that comes after ExceptionClass is one of that list: ValueError, IOError, etc.

try/except semantics If the code in the body raises the specified exception: The body stops executing immediate (like a “goto”) Doesn’t even finish the current line! Then Python runs the except block (instead of crashing) After the body (called the handler) is finished, go on to the code that follows You can have several except blocks for different exceptions for the same try block. Or one block for more than one exception: except (ValueError, IndexError):

An exception example How do we get the input and convert it to a number? number = float(input(“please enter a number: “)) float(…) raises a ValueError on a non-numeric input Use try/except block.

Hints for catching exceptions Have a plan! If you don’t know how to fix the error, don’t catch it It’s better to crash than to continue with bad data Keep the try block as small as possible It should contain the line that might raise the exception And subsequent lines that depend on its success Don’t duplicate code in the try and except blocks That code belongs after the try/except, so it happens either way Don’t wrap the whole main in a try! The program probably won’t know which error happened or how to fix it

Dealing with large amounts of data Some programs need a lot of data. How do you handle it? Hard code it into the source? That’s hard to change if you’re not a programmer Ask the user to type it in each time the program runs? If it’s more than a few pieces, your users will refuse! Or make typos! Store your data in an external file! Can be as big as your device can hold! When you type your program, you save it in a file. Data can be saved too!

Why use files to hold data? They’re easier to edit than a source file Files persist across runs of your program And across reboots of your operating system They can hold LARGE amounts of data (more than will fit in RAM!) You can use the same data file as input for different programs You can use the output of one program as input to another

Input/output with the user

I/O with files

Using files As in other programs (word processors, IDEs, etc.) you must open a file before you can use it in your program. Create a file object in your program that represents the file on disk You can read from or write to the object depending on the mode “r”,”w”,”a” Input or output comes from the file instead of from the user Syntax: fileobj = open(filename, “r”) # r for reading (input) fileobj = open(filename) # default is reading fileobj = open(filename, “w”) # w for writing fileobj is a variable that will hold the file object filename is a string that names the file (can be a constant or a variable)

Filenames The default location for a data file is the current directory, i.e., the file your py file is saved in You can specify an absolute path if you want open(“C:\\Users\\me\\input.txt”)

IOError If we are trying to read from a file, what can go wrong? Maybe the file isn’t there Or it’s there but you don’t have permissions to access it Or you do but then your hard drive crashes In these situations, opening a file raises an IOError exception Renamed to OSError in Python 3.4 You can catch the exception just like any other But there’s no point in trying to open again with the same filename A common fix is to ask the user for another filename

Exception while opening try: fn = input(“Enter a filename: “) infile = open(fn, “r”) except IOError: print(“Could not open” , fn)

Looping over the lines in a file The simplest way to use an input file once you have successfully opened it is to loop over the lines in the file A file object can be used as a sequence of lines for a for loop: for line in myfile: Each line is a string delimited by a newline character. myfile is a file object, NOT a filename! When you’re finished with a file, make sure to close the file! Frees up resources associated with the file If you don’t, the file will take up memory until the program exits … if not longer than that!

Text files: characters and bytes Two type of files: Text file stores a sequence of characters Binary file stores a sequence of bytes

Text files: what is a line? So if a text file is a sequence of characters, what’s a line? A sequence of characters delimited by the newline character Newline (‘\n’) is the line delimiter

Sequential and random access Sequential access: reading or writing the file in order starting from the beginning and going to the end Similar to how a for loop works on a list or string Read the first line, then the second line, etc. No skipping, no backing up! Random access: reading or writing without regard to order “Go to byte 7563 and put a 7 there” Like lists: we can use mylist[5] without having to go through indexes 0 through 4 first Also called direct access

Sequential and random access Text files usually use sequential access Binary files can use either sequential or random access

Reading a line at a time Here’s one way to read a line at a time for line in file: This technique is very useful but a little inflexible: It only lets us use one line per iteration But what if our file looked like this? Student 1 Score 1 … The readline method lets you control reading more precisely. line = infile.readline()

Readline line = infile.readline() This reads a single line from the file Returns a string, including the newline at the end The next time you do input, you get the next line Even if you use a different input technique next time

Reading a whole file at once Python also gives us two methods that read in the whole file at once That’s much easier if we need to process the contents out of order Or if we need to process each line several times But it doesn’t work if the file is larger than RAM. readlines gives us the whole file as a list of strings (note the s on the end of the name) line_list = infile.readlines() infile.close() #close, the file is exhausted for line in line_list:

Reading a whole file another way read gives us the whole (rest of the) file as a single string newlines and all content = infile.read() infile.close() As with readlines, you might as well close it immediately What to do with the string? You can use split line_list = content.split(“\n”) Unlike other methods, this gives you a list of strings without newlines Because split removes the delimiter

Output files It’s possible to write to files too First, open the file for writing: outfile = open(“out.txt”, “w”) # w for write mode If the file does not already exist, it creates it If the file already exists, truncates it to zero bytes!! Cuts everything out of the file to start over The old data in the file is lost forever! Opening for writing is both creative and destructive. You can also open a file for appending: logfile = open(“audit.log”, “a”) # a for append Adds to the end of an existing file – no truncation, no data lost If the file doesn’t exist, will still create it. Which to use? Do you want to add to the file or overwrite it?

Open modes summary Mode Letter File exists File doesn’t exist Read r OK IOError Write w Truncates the file Creates the file Append a OK (adds to the end)

Writing to an output file There are two ways to write to an output file: You can use the same print function that we’ve been using Just give an extra argument at the end, file = fileobj print(“Hello,”, name, file = outfile) You can also write a string with the write method Takes one string argument (nothing else!) outfile.write(“Hello, world!\n”) Does not automatically add a newline character!

Closing files You should close a file when you are finished with it outfile.close() Frees resources (like the file buffer!) When to close a file? As soon as you know you don’t have to read / write it again Immediately after your loop With read or readlines, immediately after that statement

Files versus lists Files are Lists are Permanent “persistent” Bigger (must fit on your device) Slower to access Sequential-access (as we do it in this class) Lists are Temporary Smaller (must fit in memory) Faster to access Random or sequential access