File Handling in Python
Learn how to perform essential file operations in Python. This guide covers reading from and writing to files, understanding file modes, and practical data logging techniques for persistent data storage.
import { Callout } from '@/components/callout';
File Handling in Python
Nearly all real-world applications need to work with data that persists even after the program closes. The most fundamental way to save and retrieve information is by interacting with files on the computer's storage. Python provides a simple and powerful set of built-in functions for creating, reading, updating, and deleting files.
The open()
Function: Your Gateway to Files
The core function for all file input/output (I/O) operations is open()
. This function creates a file object, which serves as a link to a file residing on your disk. At its simplest, open()
takes two parameters: the filename
and the mode
.
file_object = open("filename.txt", "mode")
Understanding File Modes
The mode is a string that tells Python what you intend to do with the file. Choosing the right mode is critical as it determines your permissions and how the file is treated.
Mode | Name | Description | Behavior if File Doesn't Exist |
---|---|---|---|
"r" | Read | Opens a file for reading. This is the default mode. | Throws an error. |
"w" | Write | Opens a file for writing. Warning: If the file exists, its contents are erased before writing. | Creates the file. |
"a" | Append | Opens a file for appending. New data is written to the end of the file. | Creates the file. |
"x" | Create | Creates the specified file. | Throws an error if the file already exists. |
Additionally, you can specify whether the file should be handled in text or binary mode:
"t"
(Text): The default mode. Data is handled as strings (text)."b"
(Binary): Used for non-text files like images or executable files.
You combine these, for example, using "rb"
to read a binary file. If you only specify "r"
, Python assumes "rt"
.
Writing and Creating Files
To write data to a file, you must open it in a mode that permits writing, such as 'w'
, 'a'
, or 'x'
. After opening the file, you use the .write()
method on the file object.
<Example title="Creating a New File and Writing to It">
This example uses the 'x'
mode to create a new file. If myfile.txt
already exists, this code will raise an error.
# The 'x' mode creates a new file for writing
f = open("myfile.txt", "x")
data = "Hello World"
f.write(data)
# It is crucial to close the file to save the changes
f.close()
</Example>
<Warning title="Always Close Your Files!">
When you are finished with a file, you must call the .close()
method. Closing a file finalizes any writes, flushes buffers, and releases the file resource from your program's control. Forgetting to close a file can lead to data corruption or loss.
</Warning>
Reading From Files
To read the contents of a file, you open it in read mode ('r'
). The .read()
method will read the entire contents of the file and return it as a single string.
<Example title="Reading an Entire File"> This code opens the file we created earlier and prints its contents to the console.
# The 'r' mode opens an existing file for reading
f = open("myfile.txt", "r")
# .read() gets the entire content of the file
content = f.read()
print(content)
# Output: Hello World
f.close()
</Example>
Practical Application: Data Logging
A very common use case for file handling is data logging, where a program records events or measurements over time. For this, you typically append new entries to an existing file.
Writing Log Data
Let's simulate logging a series of data points. We'll open a file in append mode ('a'
) and write each value on a new line. The newline character (\n
) is essential for structuring the log file.
<Example title="Logging Data to a File">
data_points = [1.6, 3.4, 5.5, 9.4]
# 'a' mode will create the file if it doesn't exist, or append to it if it does
f = open("datalog.txt", "a")
for value in data\_points:
# Convert the number to a string before writing
record = str(value)
f.write(record)
f.write("\\n") \# Write a newline to separate entries
f.close()
Reading Log Data
To read the structured log file, you can iterate directly over the file object. This reads the file line by line, which is very memory-efficient for large files.
data_read = [] for record in f:
Each 'record' string includes the newline character, so we remove it
clean_record = record.strip() # .strip() is often better than .replace() data_read.append(float(clean_record)) # Convert back to a number
f.close()
print(data_read)
Output: [1.6, 3.4, 5.5, 9.4]
</Example>
## Exercises
<Exercise title="Data Logging">
You have the following data representing measurements over time:
- Time 1, Value 22
- Time 2, Value 25
- Time 3, Value 28
1. Write a Python script to log this data to a comma-separated file (`data.csv`). Each line should be in the format `time,value` (e.g., `1,22`).
2. Write a second Python script that reads the `data.csv` file and prints each time and value to the console.
</Exercise>
<Exercise title="Sensor Data Simulation and Plotting">
1. Write a script that simulates reading a temperature sensor every 10 seconds for a total of 5 minutes (30 readings).
2. For each reading, generate a random integer temperature between 20 and 30. (Hint: use `import random` and `random.randint(20, 30)`).
3. Log both the time (e.g., 0, 10, 20, ...) and the temperature value to a file named `temperature_log.csv`.
4. Write a second script that reads the data from `temperature_log.csv` and uses `matplotlib` to plot the temperature over time.
</Exercise>