Head First Python Chapter 4 Persistence: Saving Data to Files SNU IDB Lab.

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 01 / 20 / 2009 Instructor: Michael Eckmann.
Advertisements

1 Exception-Handling Overview Exception: when something unforeseen happens and causes an error Exception handling – improves program clarity by removing.
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 8 Overview – Learn to use try catch blocks – Learn to use streams – Learn to use text files.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then.
Exceptions COMPSCI 105 S Principles of Computer Science.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
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.
The NetBeans IDE CSIS 3701: Advanced Object Oriented Programming.
Introduction to Python
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
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.
Creating your first C++ program
Dealing with Errors. Error Types Syntax Errors Runtime Errors Logical Errors.
COMP Exception Handling Yi Hong June 10, 2015.
Indexed and Relative File Processing
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter 9 I/O Streams and Data Files
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 7 Files.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Cem Sahin CS  There are two distinguishable kinds of errors: Python's Errors Syntax ErrorsExceptions.
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
Chapter 5 Files and Exceptions I. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a file? A.
Verification & Validation. Batch processing In a batch processing system, documents such as sales orders are collected into batches of typically 50 documents.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
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.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
PC204 Lecture 5 Conrad Huang Genentech Hall, N453A
Error Handling Tonga Institute of Higher Education.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Head First Python: Ch 3. Files and Exceptions: Dealing with Errors Aug 26, 2013 Kyung-Bin Lim.
Sequential Processing to Update a File Please use speaker notes for additional information!
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Exception Handling. VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Exceptions in Python Error Handling.
Introduction to Computing Science and Programming I
Fundamentals of Python: First Programs
Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Error Correcting Code.
Binary Files.
Exceptions and files Taken from notes by Dr. Neil Moore
File Handling Programming Guides.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Exceptions and files Taken from notes by Dr. Neil Moore
Fundamentals of Data Structures
ERRORS AND EXCEPTIONS Errors:
Using Text Files in Python
Strings and Serialization
Python Syntax Errors and Exceptions
Topics Introduction to File Input and Output
By Ryan Christen Errors and Exceptions.
Exception Handling COSC 1323.
Topics Introduction to File Input and Output
IST256 : Applications Programming for Information Systems
Presentation transcript:

Head First Python Chapter 4 Persistence: Saving Data to Files SNU IDB Lab.

Outline Programs produce data From the previous class… Open your file in write mode Example - write mode Files are left open after an exception! Extend try with 'finally' Knowing the type of error is not enough Example - Error types Use 'with' to work with files Example - with Pickle your data Example - pickle

Programs produce data Typically, programs: save processed data print output on screen transfer data over network This chapter will focus on storing and retrieving file data

From the previous class…

Open your file in write mode

Example – write mode (1)

Example – write mode (2) Before the program runs, there are no data files in your folder, just your code After your program runs, two text files are created in your folder, man_data.txt and other_data.txt

Files are left open after an exception! If IOError is handled before a file is closed, written data might Become corrupted We still need to close files no matter what

Extend try with finally No matter what, the finally suite always runs.

Knowing the type of error is not enough I/O Error is displayed as a generic “File Error” What really happened? When an error occurs at runtime, Python raises an exception of the specific type (IOError, ValueError, etc.) Python creates an exception object that is passed as an argument to your except suite

Example – Error types (1) The file doesn’t exist, so its object was not created Impossible to call close(), so the program ends up with NameError

Example – Error types (2) Quick fix: add a check to see if the file object exists Still, we are none the wiser as to what the error is. So, we add

Example – Error types (3) Another error! This time it’s a TypeError. Strings and objects are not compatible. Use str() …and get the correct output,

Use with to work with files Instead of the try/except/finally pattern, Python offers the with statement, which can shorten code with automatically closes opened files

Example - with

Pickle your data Python offers a standard library called pickle, which can save/load almost any Python data object, including lists Once pickled, the data is persistent and ready to be read into another program at a later time

Example – pickle (1)

Example – pickle (2) new_man = [] try: with open(‘man_data.txt’, ‘rb’) as man_file:rb stands for “readable, binary”. new_man = pickle.load(man_file) except IOError as err: print(‘File error: ‘ + str(err)) except pickle.PickleError as perr: print(‘Pickling error: ‘ + str(perr)) print(new_man)