💾File I/O
File Input / Output (I/O) is a fundamental concept in programming that involves reading data from and writing data to external files.
It is recommended that you read the following article from realpython.
Benefits
Data Persistence: File I/O allows programs to store data persistently on disk, ensuring information is retained across program runs.
Data Sharing: It provides a means for different programs and platforms to exchange data efficiently.
Configuration and Logging: File I/O is essential for storing program configurations and logging events and errors.
Data Import/Export: Files enable data import/export in various formats, making it valuable for data analysis and interactivity.
Resource Management and Security: Properly managing file resources is essential, and file permissions ensure data security from unauthorized access.
In Python, the open()
function can read plain text files without the need to import additional modules. You can read files with the following extensions using open()
without importing any additional modules:
.txt
: Plain text files..log
: Log files..ini
: INI configuration files..cfg
,.conf
: Configuration files..dat
: Generic data files.
For these file types, you can use open()
to read the content line by line or as a whole, depending on your needs.
Example: Reading a .txt
file
.txt
fileHow to do File I/O in Python
Note
open()
creates a file object in Python that links an external file to your python scriptwith open() as label:
creates a new code block where you can interact with the file object created byopen()
.We use this nomenclature to let the with statement close our files if we exit the code block.
Closing a file object in Python is important because it releases system resources (such as file handles) associated with the file, ensuring that other programs can access it. Failing to close a file properly may lead to resource leaks and potential data corruption, especially when writing to a file, as data may not be flushed from memory to disk until the file is closed.
open()
different modes
open()
different modesIn this situation, we set our mode as "r"
for reading. Our intended purpose with the file is to obtain data from the file.
In this situation, we set our mode as "w"
for reading. Our intended purpose with the file is to overwrite an existing data from the file.
If the "example2.txt"
file did not exist in the same directory as the python script, it will create a new file entitled "example2.txt".
In this situation, we set our mode as "a"
for appending. Our intended purpose with the file is to add data starting from the end of the file. If the file does not exist, it will be created.
How to read data from a file.
.read()
:This method reads the entire content of the file as a single string.
You can specify the number of characters to read as an optional argument, but if omitted, it reads the entire file.
.readline()
:This method reads a single line from the file each time it's called.
It returns an empty string when it reaches the end of the file.
.readlines()
:This method reads all lines of the file and returns them as a list of strings.
Each element in the list represents a line from the file.
It's important to note that these methods maintain a position in the file, so consecutive calls to .readline()
will read successive lines, and calling .read()
or .readlines()
again will continue from where the file pointer is located. To reset the file pointer, you can use .seek()
to change the file's position.
How to write new data onto a file.
Open the File in Write Mode: First, you need to open the file in write mode ('w'). If the file doesn't exist, it will be created. If it does exist, its contents will be truncated (deleted).
Use
.write()
to Write Data: You can use the.write()
method to write data to the file. It takes a single string argument that represents the data you want to write.Close the File: After writing data, it's a good practice to close the file using the
with
statement or the.close()
method to ensure that changes are flushed to the file and resources are released.
This code will create or overwrite the file "example.txt" with the specified text. If the file already exists, the content will be replaced, so be cautious when using 'w' mode to avoid unintentional data loss.
To avoid deleting the contents of a file and add on data instead, the mode should be set to "a"
for your open()
mode.
Last updated