Python provides robust support for managing files, allowing users to perform operations such as reading from and writing to files with ease. The ability to handle files is crucial, particularly when there is a need to retain data permanently within a file. A file serves as a designated space on the disk to hold associated data. This stored information remains accessible (non-volatile) even after the program has concluded.
In Python, files can be handled in two distinct modes: text mode or binary mode. A file can exist in either text or binary format, with each line concluding with a specific character, such as a comma (,) or a newline character. Python processes code sequentially, executing one line at a time before prompting the interpreter to begin the next line. This operation is an ongoing cycle within the Python environment.
Why do we need File Handling?
We need file handling for various purposes such as:
- We can safely save our data even after the program terminates.
- We can access various types of files, like .txt, .json, and .csv files.
- Without using a lot of memory, we can manage large files much efficiently.
- Using the file handling, we can automate various tasks such as reading configs or saving the output.
- We can also handle input/output operations in real-world applications and tools using file handling.
A file operation can be done in the following order:
- Open a file
- Read or write - which is a performing operation
- Close the file
1. Opening a file
A file operation begins with the process of opening the file. Initially, you need to open the File, after which Python will commence the operation. The opening of a file is accomplished using the open function in Python. This function requires two parameters: the name of the file and the mode of access that dictates how the file is to be interacted with. It is essential to define the mode when invoking the open function. Upon execution, the function provides a file object, which can then be utilized to carry out a range of tasks, such as reading and writing.
Syntax:
It has the following syntax:
file object = open(<file-name>, <access-mode>, <buffering>)
Files can be accessed through multiple modes such as reading, writing, or appending. Below are the specifics regarding the access modes utilized when opening a file.
| Access mode | Description |
|---|---|
r |
r means to read. So, it opens a file for read-only operation. The file pointer exists at the beginning. The file is, by default, open in this mode if no access mode is passed. |
rb |
It opens the file in read-only mode inbinaryformat. The file pointer exists at the beginning of the file. |
r+ |
It opens the file to read and write both. The file pointer exists at the beginning of the file. |
rb+ |
It opens the file to read and write in both binary formats. The file pointer exists at the beginning of the file. |
w |
It opens the file to write only. It overwrites the file if it previously exists or creates a new one if no file exists with the same name. The file pointer exists at the beginning of the file. |
wb |
It opens the file to write only in binary format. It overwrites the file if it exists previously or creates a new one if no file exists. The file pointer exists at the beginning of the file. |
w+ |
It opens the file to write and read. It is different from r+ in the sense that it overwrites the previous file if one exists, whereas r+ doesn't overwrite the previously written file. It creates a new file if no file exists. The file pointer exists at the beginning of the file. |
wb+ |
It opens the file to write and read both in binary format. The file pointer exists at the beginning of the file. |
a |
It opens the file in append mode. The file pointer exists at the end of the previously written file if it exists. It creates a new file if no file exists with the same name. |
ab |
It opens the file in append mode in binary format. The pointer exists at the end of the previously written file. It creates a new file in binary format if no file exists with the same name. |
a+ |
It opens a file to append to and read from. The file pointer remains at the end of the file if a file exists. It creates a new file if no file exists with the same name. |
ab+ |
It opens a file to append and read both in binary format. The file pointer remains at the end of the file. |
Python Example for Opening a file
Let's consider an example that illustrates the process of opening a file in Python.
#opens the file file.txt in read mode
fileptr = open("file.txt","r")
if fileptr:
print("file is opened successfully")
Output:
<class '-io.TextIOWrapper'>
The file is opened successfully
Explanation:
In the code provided above, the filename is supplied as the initial argument, and the file is accessed in read mode by specifying 'r' as the second argument. The variable fileptr contains the file object, and if the file is opened without issues, the subsequent print statement will be executed.
2. Reading a File
To access the contents of a file with a Python script, the language offers the read function. This function retrieves a string from the specified file. It is capable of processing data in both text and binary formats.
Syntax:
It has the following syntax:
fileobj.read(<count>)
In this context, the count refers to the total number of bytes that will be read from the file, commencing from the file's start. If the count parameter is omitted, the operation may read through the file's entire content until it reaches the end.
Python Example for read Method
To illustrate the process of reading a file using Python, let’s consider a practical example.
#open the file.txt in read mode. Causes an error if no such file exists.
fileptr = open("file2.txt","r")
#stores all the data of the file into the variable content
content = fileptr.read(10)
# prints the type of the data stored in the file
print(type(content))
#prints the content of the file
print(content)
#closes the opened file
fileptr.close()
Output:
<class 'str'>
Python is
Explanation:
In the code presented above, the content of file2.txt has been accessed utilizing the read method. We specified a count parameter of ten, indicating that the method will retrieve the initial ten characters from the file.
Read Lines of the file
Python provides the capability to read a file one line at a time through the use of the readline method. This method retrieves the lines from the file starting at the very beginning.
As an illustration, invoking the readline function twice enables us to retrieve the initial two lines from the file. In essence, the readline method facilitates the reading of a single line at any given moment.
Python Example to Read Lines of the File:
To illustrate the process of reading lines from files with the readline function in Python, we can consider the following example.
#open the file.txt in read mode. It may cause an error if no such file exists.
fileptr = open("file2.txt","r");
#stores all the data of the file into the variable content
content = fileptr.readline()
content1 = fileptr.readline()
#prints the content of the file
print(content)
print(content1)
#closes the opened file
fileptr.close()
Output:
Python is a modern-day language.
It makes things so simple.
Explanation:
We invoked the readline function two times; consequently, it retrieved two lines from the file. This implies that if you invoke the readline function n times within your program, it will read n lines from the file. This demonstrates the functionality of the readline function in Python. Additionally, Python offers the readlines method, which serves the purpose of reading multiple lines. It returns a list of lines until the end of the file (EOF) is encountered.
Python Example for Write Mode
This is a write operation in Python. We begin by opening an already existing file using the provided code, after which we proceed to write to it. Below is an example to illustrate this:
file = open('file.txt','w')
file.write("Here we write a command")
file.write("Hello users of our site")
file.close()
Output:
> Hi
ERROR!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Hi' is not defined
3. The close Method
The close method serves the purpose of concluding the program's execution. After completing all tasks involving the file, it is essential to close it via the Python script by utilizing the close method. Any data that has not been written will be lost when the close method is invoked on the file object. It is considered a best practice to ensure the file is closed after all operations have been finalized.
Syntax:
It has the following syntax:
fileobject.close()
Python Example for Closing Method
Let's consider an example to illustrate the process of closing a file in Python.
# opens the file file.txt in read mode
fileptr = open("file.txt","r")
if fileptr:
print("The existing file is opened successfully in Python")
#closes the opened file
fileptr.close()
Once the file is closed, no further operations can be executed on it. It is essential to ensure that the file is closed correctly. If an exception arises during any file operations, the program may end abruptly without properly closing the file. To address this issue, we should implement the following method.
try:
fileptr = open("file.txt")
# perform file operations
finally:
fileptr.close()
4. The with statement
The with statement made its debut in Python version 2.5. This statement proves to be advantageous when handling files. It is particularly beneficial in situations where two statements need to be executed with a block of code situated in between them.
Syntax:
It has the following syntax:
with open(<file name>, <access mode>) as <file-pointer>:
#statement suite
Python Example to demonstrate with Statement
To illustrate the usage of the with statement in Python file manipulation, let's consider an example.
with open("file.txt", 'r') as f:
content = f.read();
print(content)
File Related Methods
The file object includes a variety of methods that allow for the management of files across different operating systems.
| Methods | Description |
|---|---|
| file.close() | It closes the open file. Once the file is closed, it can't be read or written to anymore. |
| File.fush() | It flushes the internal buffer. |
| File.fileno() | It returns the file descriptor used by the underlying implementation to request I/O from the OS. |
| File.isatty() | It returns true if the file is connected to a TTY device; otherwise, it returns false. |
| File.next() | It returns the next line from the file. |
| File.read([size]) | It reads the file for the specified size. |
| File.readline([size]) | It reads one line from the file and places the file pointer to the beginning of the new line. |
| File.readlines([sizehint]) | It returns a list containing all the lines of the file. It reads the file until the EOF occurs using the readline() function. |
| File.seek(offset[,from) | It modifies the position of the file pointer to a specified offset with the specified reference. |
| File.tell() | It returns the current position of the file pointer within the file. |
| File.truncate([size]) | It truncates the file to the optional specified size. |
| File.write(str) | It writes the specified string to a file |
| File.writelines(seq) | It writes a sequence of strings to a file. |
Conclusion
In this guide, we will provide an overview of file management in Python. Python allows users to effectively manage files, including reading from and writing to them. In this tutorial, we will explore different functions available in Python that facilitate the processes of reading, writing, deleting, and opening files. Each of these functions will be illustrated with examples, their corresponding outputs, and comprehensive explanations.
Python File Handling FAQs
1. How can we open a file in Python?
In Python, we can utilize the built-in open function to access a file. Let’s explore the process:
f = open("file.txt", "r") # "r" = read mode
In this instance, we need to substitute "file.txt" with the actual name of the file.
2. What are the different file modes available in Python?
There are various file modes available in Python, such as:
- "r" → Read (default, error if file doesn't exist)
- "w" → Write (creates new or overwrites existing file)
- "a" → Append (adds data at the end of the file)
- "x" → Create (error if file already exists)
- "b" → Binary mode (e.g., "rb", "wb")
- "t" → Text mode (default, e.g., "rt", "wt")
3. How do we read from a file?
To access data from a file, we can utilize the "r" mode along with the read function, readline function, and readlines function. Let's explore this further with an illustrative example:
Code:
with open("file.txt", "r") as f:
content = f.read() # To Read the entire file
line = f.readline() # To Read one line
lines = f.readlines() # To read all lines into a list
4. How do we write to a file?
To write data to a file, we can utilize the "w" mode in conjunction with the write and writelines functions. Let’s explore this with an example in code:
Code:
with open("file.txt", "w") as f:
f.write("Hello World\n")
f.writelines(["Line1\n", "Line2\n"])
5. How can we read/write a CSV file?
import csv
# Write to a CSV file
with open("data.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerow(["Name", "Age"])
writer.writerow(["Example", 14])
# Read to a CSV file
with open("data.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
print(row)