Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.

Slides:



Advertisements
Similar presentations
Chapter 9 TRAP Routines and Subroutines. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 Subroutines.
Advertisements

Python Programming Chapter 1: The way of the program Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Chapter 5: Loops and Files.
Guide To UNIX Using Linux Third Edition
Chapter 15 Testing. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Why Testing? In chapter 14, we.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
Chapter 1 Beginnings.
Systems Software & Operating systems
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 13 Files and Exception Handling 1.
Topics Introduction Hardware and Software How Computers Store Data
Introduction to Python
November 15, 2005ICP: Chapter 7: Files and Exceptions 1 Introduction to Computer Programming Chapter 7: Files and Exceptions Michael Scherger Department.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Chapter 14 Files and Exceptions II. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What we already.
The Study of Computer Science Chapter 0 Intro to Computer Science CS1510.
Structured programming 4 Day 34 LING Computational Linguistics Harry Howard Tulane University.
General Programming Introduction to Computing Science and Programming I.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Chapter 11 Introduction to Classes. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 11.1.
Python From the book “Think Python”
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Indexed and Relative File Processing
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
Now, please open your book to page 60, and let’s talk about chapter 9: How Data is Stored.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
1 The Practice of Computing Using PYTHON William PunchRichard Enbody Chapter 3 Algorithms & Program Development Copyright © 2011 Pearson Education, Inc.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
File I/O Michael Ernst UW CSE 140 Winter File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Chapter 9 Dictionaries and Sets.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Files Tutor: You will need ….
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.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
File I/O Ruth Anderson UW CSE 140 Winter File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
1. COMPUTERS AND PROGRAMS Rocky K. C. Chang September 6, 2015 (Adapted from John Zelle’s slides)
Programming Objectives What is a programming language? Difference between source code and machine code What is python? – Where to get it from – How to.
CSV Files Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Files Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
CS 177 Recitation Week 1 – Intro to Java. Questions?
CHARACTER INPUT / OUTPUT AND INPUT VALIDATION. Introduction Input and output devices: keyboards, disk drives, mouse, monitors, printers. I/O functions.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
AS Level Computing 8 CHAPTER: Fundamentals of structured programming The basics of good programming Algorithms System Flow Charts Symbol Conventions Steps.
Introduction to Computing Science and Programming I
Agenda Introduction Computer Programs Python Variables Assignment
CSC201: Computer Programming
Introduction to Python
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.
Logger, Assert and Invariants
Containers and Lists CIS 40 – Introduction to Programming in Python
Topics Introduction to Repetition Structures
Exceptions and files Taken from notes by Dr. Neil Moore
File Handling Programming Guides.
Topics Introduction to File Input and Output
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Exceptions and files Taken from notes by Dr. Neil Moore
Programming.
A First Program.
Topics Introduction to File Input and Output
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Presentation transcript:

Chapter 5 Files and Exceptions I

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A file is a collection of data that is stored on secondary storage like a disk or a thumb drive accessing a file means establishing a connection between the file and the program and moving data between the two

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Two types of files Files come in two general types: text files. Files where control characters such as "/n" are translated. This are generally human readable binary files. All the information is taken directly without translation. Not readable and contains non-readable info.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. File Objects or stream When opening a file, you create a file object or file stream that is a connection between the file information on disk and the program. The stream contains a buffer of the information from the file, and provides the information to the program

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Buffering Reading from a disk is very slow. Thus the computer will read a lot of data from a file in the hopes that, if you need the data in the future, it will be buffered in the file object. This means that the file object contains a copy of information from the file called a cache (pronounced "cash")

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Making a file object my_file = open("my_file.txt", "r") my_file is the file object. It contains the buffer of information. The open function creates the connection between the disk file and the file object. The first quoted string is the file name on disk, the second is the mode to open it (here, "r" means to read)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Where is the disk file? When opened, the name of the file can come in one of two forms: "file.txt" assumes the file name is file.txt and it is located in the current program directory "c:\bill\file.txt" is the fully qualified file name and includes the directory information

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Different modes

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Careful with write modes Be careful if you open a file with the 'w' mode. It sets an existing file’s contents to be empty, destroying any existing data. The 'a' mode is nicer, allowing you to write to the end of an existing file without changing the existing contents

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Text files use strings If you are interacting with text files (which is all we will do in this book), remember that everything is a string –everything read is a string –if you write to a file, you can only write a string

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. writing to a file Once you have created a file object, opened for reading, you can use the print command you add file=file to the print command

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. close When the program is finished with a file, we close the file flush the buffer contents from the computer to the file tear down the connection to the file close is a method of a file obj file_obj.close() All files should be closed!

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 5.1 Reverse file lines

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Exceptions First Cut

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. How to deal with problems Most modern languages provide methods to deal with ‘exceptional’ situations Gives the programmer the option to keep the user from having the program stop without warning Again, this is not about fundamental CS, but about doing a better job as a programmer

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What counts as exceptional errors. indexing past the end of a list, trying to open a nonexistent file, fetching a nonexistent key from a dictionary, etc. events. search algorithm doesn’t find a value (not really an error), mail message arrives, queue event occurs

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. exceptions (2) ending conditions. File should be closed at the end of processing, list should be sorted after being filled weird stuff. For rare events, keep from clogging your code with lots of if statements.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Error Names Errors have specific names, and Python shows them to us all the time. You can recreate an error to find the correct name. Spelling counts!

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a kind of non-local control Basic idea: keep watch on a particular section of code if we get an exception, raise/throw that exception (let it be known) look for a catcher that can handle that kind of exception if found, handle it, otherwise let Python handle it (which usually halts the program)

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Doing better with input In general, we have assumed that the input we receive is correct (from a file, from the user). This is almost never true. There is always the chance that the input could be wrong Our programs should be able to handle this.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Worse yet, input is evil "Writing Secure Code”, by Howard and LeBlanc – “All input is evil until proven otherwise” Most security holes in programs are based on assumptions programmers make about input Secure programs protect themselves from evil input

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Rule 7 All input is evil, until proven otherwise

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. General form, version 1 try: suite except a_particular_error: suite

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. try suite the try suite contains code that we want to monitor for errors during its execution. if an error occurs anywhere in that try suite, Python looks for a handler that can deal with the error. if no special handler exists, Python handles it, meaning the program halts and with an error message as we have seen so many times 

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. except suite an except suite (perhaps multiple except suites) is associated with a try suite. each exception names a type of exception it is monitoring for. if the error that occurs in the try suite matches the type of exception, then that except suite is activated.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. try / except group if no exception in the try suite, skip all the try / except to the next line of code if an error occurs in a try suite, look for the right exception if found, run that except suite and then skip past the try / except group to the next line of code if no exception handling found, give the error to Python

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Code Listing 5.2 Find a line in a file

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc.

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Counting Poker Hands

"The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Reminder, rules so far 1.Think before you program! 2.A program is a human-readable essay on problem solving that also happens to execute on a computer. 3.The best way to imporve your programming and problem solving skills is to practice! 4.A foolish consistency is the hobgoblin of little minds 5.Test your code, often and thoroughly 6.If it was hard to write, it is probably hard to read. Add a comment. 7.All input is evil, unless proven otherwise.