10
FILES AND E XCEP TIONS
Now that you’ve mastered the basic skills
you need to write organized programs that
are easy to use, it’s time to think about making
your programs even more relevant and usable. In
this chapter, you’ll learn to work with files so your pro-
grams can quickly analyze lots of data.
You’ll learn to handle errors so your programs don’t crash when they
encounter unexpected situations. You’ll learn about exceptions, which are
special objects Python creates to manage errors that arise while a program
is running. You’ll also learn about the json module, which allows you to save
user data so it isn’t lost when your program stops running.
Learning to work with files and save data will make your programs easier
for people to use. Users will be able to choose what data to enter and when
to enter it. People will be able to run your program, do some work, and then
close the program and pick up where they left off. Learning to handle excep-
tions will help you deal with situations in which files don’t exist and deal with
other problems that can cause your programs to crash. This will make your
programs more robust when they encounter bad data, whether it comes from