Reading a file in Python is a lot easier than you think.
To read a file in Python, you can use the built-in open() function.
This function allows you to open files, read the content and even write anything you want.
The open() function reads the file and returns the file object.
Here is the basic syntax to do this:
File_object = open("File_Name", "Access_Mode")
the File_object is the object in which the content of your file will be stored.
File_Name is the file that you want to manipulate.
Now let’s discuss access modes in detail.
Access Modes
The open()
function in Python can take several different access modes as a second argument.
These access modes determine how the file will be opened and what can be done with it.
Here are some most common access modes:
Read Mode “r”
Read mode is the default mode for reading a file in Python.
Using the read mode, you can open a file for reading.
Keep in mind, while using this access mode, you can only read the file and can’t modify it.
When a file is opened, the handle is at the beginning of the file.
An I/O error is raised if the file does not found.
You can use “r” in the open function to open the file in the read mode.
file = open("myfile.txt", "r")
Write mode “w”
Write mode is used to write in a file.
You can open a file with write mode.
If the file already exists, the previous content will be wiped out.
And you can write new content.
If the file does not exist, a new file will be created.
You can use “w” in the open function to open the file in the write mode.
file = open("myfile.txt", "w")
Read and Write mode “r+”
As the name suggests, the read and write mode will allow you to read a file in Python and write it at the same time.
In this mode, The handle is at the beginning of the file.
An I/O error is raised if the file does not found.
You can use “r+” in the open function to open a file in the read and write mode.
file = open("myfile.txt", "r+")
Append mode “a”
The append mode is very similar to the write mode.
But instead of overwriting the existing data, the new data is appended at the end of the file.
You can use “a” in the open function to open the file in the append mode.
file = open("myfile.txt", "a")
Append and Read mode “a+”
Using the append and read access mode, you can open a file and can also append new content at the end of the same file.
While you are in this mode, the handle is at the last.
However, if the file does not found, a new file is created immediately.
file = open("myfile.txt", "a+")
Reading a file in Python: How do you read a file in Python?
To read a file in Python, you can use open() as we discussed earlier.
Now you have a good knowledge of the access modes.
Now let’s have a look at the step by step process of reading a file in Python.
Step by step process of reading a file in Python
In a brief, reading a file in Python consists of three steps:
- Opening the file using the open() function.
- Reading the content of the file using read(), readline(), or readlines() methods.
- Closing the file with the close() method.
1: Opening the file using the open() function
To read a file you can use the open function.
You have to pass the file name and the access mode as the arguments.
f = open("myfile.txt", "r")
contents = f.read()
print(contents)
Output
This is a text file.
I love python.
I love python mania.
Make sure that the text file is in the same folder as your Python file.
2: Reading the content using different methods
To read the content of the text file, we have different methods such as read(), readline(), and, readlines().
Now, let’s discuss each one of the methods.
a: The read() method
You can use the read() method to read the contents of the Python file.
f = open("myfile.txt", "r")
contents = f.read()
print(contents)
Output
This is a text file.
I love python.
I love python mania.
You can pass the number of characters to read as an argument to read method.
But this is totally optional.
f = open("myfile.txt", "r")
contents = f.read(4)
print(contents)
Output
This
b: The readline() method
The readline() method will return one line from the file at a time.
f = open("myfile.txt", "r")
contents = f.readline()
print(contents)
If you call the readline method two times, you will get two lines, the first line and the second line.
f = open("myfile.txt", "r")
contents = f.readline()
print(contents)
contents = f.readline()
print(contents)
Output
This is a text file.
I love python.
You can pass the number of characters to get from the line as the argument.
Syntax
contents = f.readline(n)
Code
f = open("myfile.txt", "r")
contents = f.readline(4)
print(contents)
Output
This
As you can see, we have got the first four characters of the first line of the file.
c: The readlines() method
The readlines() method returns a list with each line of the file as a list item/
f = open("myfile.txt", "r")
contents = f.readlines()
print(contents)
Output
[‘This is a text file.’, ‘I love python.’, ‘I love python mania.’]
You can iterate through this list or use it the way you want.
f = open("myfile.txt", "r")
contents = f.readlines()
for eachline in contents:
print(eachline)
Output
This is a text file.
I love python.
I love python mania.
Or you can access any line using its index.
f = open("myfile.txt", "r")
contents = f.readlines()
print(contents[0])
print(contents[2])
Output
This is a text file.
I love python mania.
3: Closing the file with the close() method
It is not a good practice to open a file in Python and then left it open.
This will consume the system’s resources.
And sometimes, due to buffering your changes might not be visible in the file until you close the file.
The close() method will close the opened file.
Syntax
file.close()
Code
f = open("myfile.txt", "r")
contents = f.read()
print(contents)
f.close()
Output
This is a text file.
I love python.
I love python mania.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.