head first python chapter 4 persistence: saving data to files

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

Upload: landry

Post on 22-Feb-2016

32 views

Category:

Documents


0 download

DESCRIPTION

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' - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Head First Python Chapter 4 Persistence: Saving Data to Files

Head First PythonChapter 4Persistence: Saving Data to Files

SNU IDB Lab.

Page 2: Head First Python Chapter 4 Persistence: Saving Data to Files

<2/18>

OutlinePrograms produce dataFrom the previous class…Open your file in write modeExample - write modeFiles are left open after an exception!Extend try with 'finally'Knowing the type of error is not enoughExample - Error typesUse 'with' to work with filesExample - withPickle your dataExample - pickle

Page 3: Head First Python Chapter 4 Persistence: Saving Data to Files

<3/18>

Programs produce dataTypically, programs:

save processed dataprint output on screentransfer data over network

This chapter will focus on storing and retrieving file data

Page 4: Head First Python Chapter 4 Persistence: Saving Data to Files

<4/18>

From the previous class…

Page 5: Head First Python Chapter 4 Persistence: Saving Data to Files

<5/18>

Open your file in write mode

Page 6: Head First Python Chapter 4 Persistence: Saving Data to Files

<6/18>

Example – write mode (1)

Page 7: Head First Python Chapter 4 Persistence: Saving Data to Files

<7/18>

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

Page 8: Head First Python Chapter 4 Persistence: Saving Data to Files

<8/18>

Files are left open after an exception!If IOError is handled before a file is closed, written data mightBecome corrupted

We still need to close files no matter what

Page 9: Head First Python Chapter 4 Persistence: Saving Data to Files

<9/18>

Extend try with finally

No matter what, the finally suite always runs.

Page 10: Head First Python Chapter 4 Persistence: Saving Data to Files

<10/18>

Knowing the type of error is not enoughI/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

Page 11: Head First Python Chapter 4 Persistence: Saving Data to Files

<11/18>

Example – Error types (1)

The file doesn’t exist, so its object was not createdImpossible to call close(), so the program ends up with NameError

Page 12: Head First Python Chapter 4 Persistence: Saving Data to Files

<12/18>

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

Page 13: Head First Python Chapter 4 Persistence: Saving Data to Files

<13/18>

Example – Error types (3)Another error! This time it’s a TypeError. Strings and objects are notcompatible.

Use str()

…and get the correct output,

Page 14: Head First Python Chapter 4 Persistence: Saving Data to Files

<14/18>

Use with to work with filesInstead of the try/except/finally pattern, Python offers the withstatement, which can shorten code

with automatically closes opened files

Page 15: Head First Python Chapter 4 Persistence: Saving Data to Files

<15/18>

Example - with

Page 16: Head First Python Chapter 4 Persistence: Saving Data to Files

<16/18>

Pickle your dataPython offers a standard library called pickle, which can save/loadalmost any Python data object, including lists

Once pickled, the data is persistent and ready to be read intoanother program at a later time

Page 17: Head First Python Chapter 4 Persistence: Saving Data to Files

<17/18>

Example – pickle (1)

Page 18: Head First Python Chapter 4 Persistence: Saving Data to Files

<18/18>

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)