In this section, you’ll learn some essential skills that will make your programs more powerful and robust:
- Working with Files: You’ll learn how to read from and write to files, which will allow your programs to handle large amounts of data efficiently.
- Handling Errors: We’ll cover how to handle errors and exceptions gracefully, so your programs don’t crash when something unexpected happens.
- Saving Data with JSON: You’ll also learn how to use the
json
module to save and load data between program runs, making your programs more interactive and persistent.
Working with files allows your programs to store data that persists even after the program has stopped running. For example, you can:
- Analyze large datasets stored in text files.
- Save user preferences and restore them later.
- Keep records of transactions or events over time.
By reading and writing files, you enable your programs to handle more data than just what’s available during a single run.
In real-world programs, things often don’t go as planned. Files might not exist, data might be corrupt, or users might provide invalid input. Python’s exception handling allows you to deal with these situations without crashing your program.
You’ll learn to use try-except
blocks to catch errors and handle them smoothly.
try:
with open('non_existent_file.txt') as file:
content = file.read()
except FileNotFoundError:
print("Sorry, the file doesn't exist.")
try
block: The code inside this block runs normally.except
block: If an error occurs (like a missing file), this block runs, preventing the program from crashing.
Using the json
module, you can easily save data from your program to a file and load it back later. This is helpful for keeping user preferences, storing settings, or maintaining any data between runs.
import json
user_data = {'name': 'Muhammad Hashim', 'age': 24}
# Save data to a file
with open('user_data.json', 'w') as file:
json.dump(user_data, file)
# Load data from a file
with open('user_data.json') as file:
loaded_data = json.load(file)
print(loaded_data)
json.dump()
: Saves a Python object as JSON data in a file.json.load()
: Loads the JSON data from the file back into a Python object.
- Persistent data: By working with files and JSON, your program can save and load data, making it more useful for users who want to resume their work.
- Error handling: With exception handling, your programs will be more stable, even when something goes wrong.
- Better user experience: Users can interact with your program without worrying about losing their data or encountering crashes.