0% found this document useful (0 votes)
17 views28 pages

Persistent Data Storage in Python

The document discusses saving and loading data with files in Python. It covers opening files, writing to and reading from files, reading files line by line, using the pickle module to write complex data types to files, and handling exceptions with try-except blocks. Example code is provided to demonstrate writing text to a file, reading from a text file, appending to a file, writing data to a file using pickle, and reading data from a pickle file. The document concludes with proposing a to-do list program that allows users to create, display, and delete to-do items stored persistently in a file.

Uploaded by

aaa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views28 pages

Persistent Data Storage in Python

The document discusses saving and loading data with files in Python. It covers opening files, writing to and reading from files, reading files line by line, using the pickle module to write complex data types to files, and handling exceptions with try-except blocks. Example code is provided to demonstrate writing text to a file, reading from a text file, appending to a file, writing data to a file using pickle, and reading data from a pickle file. The document concludes with proposing a to-do list program that allows users to create, display, and delete to-do items stored persistently in a file.

Uploaded by

aaa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OOP With Python

Unit 01 - Files

1
Saving and Loading Data with
Files
Persistent Data Storage

2
Why do we use files?

◉ Allow persistent storage of data


◉ Data continue to exist after program is stopped or computer turned off
◉ Data easily transferred between computers

3
Opening a File

◉ Open a file with the open() function


Permission argument that specifies what
the program is allowed to do with the
file:
File Directory / Name

Argument Associated Actions

r Read

w Write only (If file does not exist, program will create a one)

r+ Read and Write (Error if file does not exist)

a Append (Can be used to create a new file) 4


Writing to and Saving a File

◉ .write() function writes a string to the file (2)


○ To store numbers, you need to convert them to strings (3)
◉ .close() function save and closes the file (5)

5
Reading a File

◉ .read() function read the file and returns content as a string (2)

6
Reading line by line

◉ .readline() function read the file one line at a time

7
Let’s Experiment with writing to and reading from a File
(Demo/Practice - 11)

◉ Write a program to:


○ write some text to a file
○ read from a text file
○ append to a text file

8
Check Point - 11

◉ Every student must be able to


○ write some text to a file
○ read from a text file
○ append to a text file
◉ For students who are waiting, try the following:
○ Update your shopping list program and have it read from and write to a
file
○ Create a to-do list program

9
The Pickle Module
A Module to help us write and read complex data to
and from files

10
What is Pickle Module?

◉ Standard file open with read and write requires everything to be in


strings
◉ Pickle modules makes it easy to store other datatypes such as numbers
(integers, floats), lists, dictionaries
◉ File written with pickle is not human readable
◉ [Link]

11
Using Pickle

◉ Import module => open file with “wb” => “Dump” data into file

12
Loading Multiple Variables/Objects

◉ To store multiple variables/objects, put them in a list, or tuple

13
Reading from a Pickle File

◉ Open file with “rb” attribute


◉ Use the .load() function

14
Let’s Experiment with using Pickle
(Demo/Practice - 12)

◉ Write a program to:


○ write some data to a file with pickle
○ read from a pickle file

15
Check Point - 12

◉ Every student must be able to


○ write data to a file using pickle
○ read data from a pickle file
◉ For students who are waiting, try the following:
○ Update your shopping list program and have it read from and write using
Pickle
○ Experiment with Pickle.
■ Another way to put in multiple data is to call the .dump() function
multiple times, before closing the file. To retrieve the data, you will
need to call .load() the same number of times 16
Additional Notes

◉ Alternate way to put in multiple objects/variables


○ Call .dump() multiple times (before closing the file)
○ Call .load() multiple times to retrieve
○ Experiment with it!
◉ Other ways to open a pickle file
○ Append
◉ Other modules for storage
○ shelve - [Link]

17
Handling Exceptions
Making your programs more robust with Try … Except

18
Human Proofing your Programs

◉ Humans are not perfect. They may enter a string when they are
suppose to enter a number.
◉ What happens if the user enters a text in the following program?

19
Try … Except

◉ Use try… except to handle exceptions


◉ Concept is similar to if ... else, just that this is only to check for
exceptions

20
Let’s Experiment with Try … Except
(Demo/Practice - 13)

◉ Write a simple program that prints out the content in a file. If the file is
not found, print out an error message.
○ Hint: Google for ‘Built-in Exceptions’ in Python

21
Check Point - 13

◉ Every student must be able to


○ Use try … except
◉ For students who are waiting, try the following:
○ Make your earlier programs more robust

22
Additional Notes on Try ... Except

◉ This is not just for human errors


◉ It is used to capture and handle all kinds of exceptions that may occur
○ Failure to read a file
○ Unexpected data type
○ And many more
◉ [Link]

23
To-Do List
Create a To-Do list program

24
To Do List Program Specifications

◉ A To Do List program which allows user to create, display and delete to


do items. The list will be stored on persistent storage. The next time the
user run the program, the to do list will still be there.
◉ Make sure your program is robust (i.e. able to handle the common
input selection errors)
◉ Teacher to demonstrate the program

25
Additional Challenges

◉ Read a text file which contains an essay and perform an analysis on the
distribution of the letters
◉ Open a url and read a webpage
○ Check out the package urllib
○ [Link]
○ Use this library to open and read a webpage

26
Reflections

27
Persistent Storage

◉ We can use files to achieve persistent storage of data


◉ Two key methods to write to file:
○ Writing as text
○ Using pickle module
◉ There are other methods to write to file
◉ There are also other methods to achieve persistent storage
○ Database
◉ Questions to Ask yourself: Why do we need persistent storage?

28

You might also like