14: Files
( \newcommand{\kernel}{\mathrm{null}\,}\)
- 14.0: Introduction
- This page discusses file handling in Python, covering how to read from and write to files and addressing potential exceptions like file not found errors. It also emphasizes effective exception handling in Python programming.
- 14.1: Reading from Files
- This page offers an overview of file handling in Python, detailing the usage of the open() function and various methods like read(), readline(), and readlines() for reading file contents. It includes practical examples, checkpoints, practice questions, and programming prompts that encourage applying the concepts, such as file reading and average calculations based on file data.
- 14.2: Writing to Files
- This page explains how to open files for writing in Python, covering modes like read ('r'), write ('w'), and append ('a'). It highlights the `write()` function's role in data writing, noting it needs string parameters and doesn't add newlines automatically. The importance of using `close()` to save changes and properly close files is emphasized. Practical examples and exercises are provided to help readers practice file writing.
- 14.3: Files in Different Locations and Working with CSV Files
- This page outlines learning objectives for accessing and processing files using Python, focusing on the open() function and the importance of file paths across different operating systems. It discusses handling CSV files and introduces libraries for working with non-Unicode formats like PDF and Word documents. The content includes a practical exercise involving the processing of a CSV file with student exam scores.
- 14.4: Handling Exceptions
- This page discusses handling file reading exceptions in Python, including `FileNotFoundError`, `IndexError`, and `ValueError`, using try/except statements. It provides examples of maintaining program flow despite errors, includes practical exercises on predicting exception outputs, and details a task for users to design a program that searches for words in a file while managing input errors.
- 14.5: Raising Exceptions
- This page discusses learning objectives for exception handling in programming, specifically with the raise statement. It describes how to signal invalid user input and the resulting execution flow during exceptions. An example featuring a Pizza class demonstrates raising a ValueError for invalid size input. Practice exercises are also included, focusing on predicting code outputs with raise and implementing a dictionary lookup function with error management.
- 14.6: Chapter Summary
- This page covers file handling in Python, focusing on the use of the open() function for reading, writing, and appending files. It underlines the need to close files for changes to be saved and discusses newline character management. CSV files are noted as a common format. The chapter also addresses error management through try and except statements, outlining exception handling and raising techniques.