Working with files is common in many Python applications, especially in data processing, configuration management, and logging. Python offers built-in libraries and modules that make it straightforward to handle various file formats, such as .txt, .csv, and .json. In this article, we’ll explore how to read and write files using Python.

 

Different modes for opening a file

In Python, when we open a file using the open() function, we must specify the mode we want to which specifies what we want to do with the file. Here are the common modes available:

Mode Description Behavior
r Read-only mode. Opens the file for reading. File must exist; otherwise, it raises an error.
rb Read-only in binary mode. Opens the file for reading binary data. File must exist; otherwise, it raises an error.
r+ Read and write mode. Opens the file for both reading and writing. File must exist; otherwise, it raises an error.
rb+ Read and write in binary mode. Opens the file for both reading and writing binary data. File must exist; otherwise, it raises an error.
w Write mode. Opens the file for writing. Creates a new file or truncates the existing file.
wb Write in binary mode. Opens the file for writing binary data. Creates a new file or truncates the existing file.
w+ Write and read mode. Opens the file for both writing and reading. Creates a new file or truncates the existing file.
wb+ Write and read in binary mode. Opens the file for both writing and reading binary data. Creates a new file or truncates the existing file.
a Append mode. Opens the file for appending data. Creates a new file if it doesn’t exist.
ab Append in binary mode. Opens the file for appending binary data. Creates a new file if it doesn’t exist.
a+ Append and read mode. Opens the file for appending and reading. Creates a new file if it doesn’t exist.
ab+ Append and read in binary mode. Opens the file for appending and reading binary data. Creates a new file if it doesn’t exist.
x Exclusive creation mode. Creates a new file. Raises an error if the file already exists.
xb Exclusive creation in binary mode. Creates a new binary file. Raises an error if the file already exists.
x+ Exclusive creation with read and write mode. Creates a new file for reading and writing. Raises an error if the file exists.
xb+ Exclusive creation with read and write in binary mode. Creates a new binary file for reading and writing. Raises an error if the file exists.

Working with .txt Files

Text files are the simplest form of files used for storing unstructured or lightly structured data.

Reading a .txt File (General Way)
Standard Way to Read .txt files
Writing to a .txt File

 

Working with .csv Files, everything will be merged

CSV (Comma-Separated Values) files are used to store tabular data. Python provides a built-in A csv module for reading and writing such files.

Reading a .csv File
Writing to a .csv File

 

Working with .json Files

JSON (JavaScript Object Notation) is commonly used for storing structured data. Python’s json module provides methods for parsing and writing JSON data.

Reading a .json File
Writing to a .json File

 

Working with .json Files Using pandas

The the pandas library is widely used for data analysis and makes reading and writing JSON files especially simple, particularly when the data is structured like a table (i.e., records or arrays of dictionaries).

Writing to a .json File

 

Conclusion

Python’s standard libraries provide efficient tools for reading from and writing to .txt, .csv, and .json files. By using simple methods such as open(), csv.reader(), and json.load(), you can handle file operations effectively in your applications. These capabilities are especially useful in data analysis, configuration management, and integration with external systems. 

read and write files using Python

Leave a Comment