While working with the Python application, you would be required to read and write text files in Python. You can refer to our other tutorial to write a text file in Python. Reading a text file in Python is a simple process that can be accomplished using a few different methods.
In this article, we will cover the following methods for reading a text file in Python:
- Using the
`open()`
function and`.read()`
method - Using the
`open()`
function and`.readlines()`
method - Using the
`with`
statement and`.read()`
method - Using the
`with`
statement and`.readlines()`
method
You can choose anyone the given methods based on your application scenario and environment. In this tutorial, I will read `myfile.txt` available in current directory that contains the following text:
cat myfile.txt
Output:Hi I'm Rahul Welcome you on tecadmin.net
Let’s take a closer look at each of these methods one by one.
Method 1: Using the `open()`
function and `.read()`
method
The first method for reading a text file in Python uses the `open()`
function and the `.read()`
method. Here is an example of how to use this method:
1 2 3 4 5 6 7 8 9 10 11 | # Open the text file in read mode file = open('myfile.txt', 'r') # Read the contents of the file into a variable contents = file.read() # Print contents value print(contents) # Close the file file.close() |
Output:Hi I'm Rahul Welcome you on tecadmin.net
In this example, we use the `open()`
function to open the text file in read mode (the ‘r’ parameter indicates that we want to read the file). Then, we use the `.read()`
method to read the contents of the file into a variable called contents. Finally, we close the file using the .close() method.
Method 2: Using the `open()`
function and `.readlines()`
method
The second method for reading a text file in Python involves using the `open()`
function and the `.readlines()`
method. This method is similar to the first method, but it returns a list of strings, where each string represents a line in the text file. Here is an example of how to use this method:
1 2 3 4 5 6 7 8 9 10 11 | # Open the text file in read mode file = open('myfile.txt', 'r') # Read the contents of the file into a list of strings lines = file.readlines() # Print the lines print(lines) # Close the file file.close() |
Output:['Hi\n', "I'm Rahul\n", 'Welcome you on tecadmin.net\n']
In this example, we use the `open()`
function to open the text file in read mode (the ‘r’ parameter indicates that we want to read the file). Then, we use the `.readlines()`
method to read the contents of the file into a list of strings called lines. Finally, we close the file using the .close() method.
Method 3: Using the `with`
statement and `.read()`
method
The third method for reading a text file in Python involves using the with the statement and the `.read()`
method. This method is similar to the first method, but it automatically closes the file after the block of code within the `with`
statement has been executed. Here is an example of how to use this method:
1 2 3 4 5 | # Open the text file in read mode using the with statement with open('myfile.txt', 'r') as file: # Read the contents of the file into a variable contents = file.read() print(contents) |
Output:Hi I'm Rahul Welcome you on tecadmin.net
In this example, we use the `with`
statement to open the text file in read mode (the ‘r’ parameter indicates that we want to read the file). The `with`
statement automatically closes the file after the block of code within the `with`
statement has been executed.
Method 4: Using the `with`
statement and `.readlines()`
method
The fourth method for reading a text file in Python involves using the `with`
statement and the `.readlines()`
method. This method is similar to the second method, but it automatically closes the file after the block of code within the `with`
statement has been executed. Here is an example of how to use this method:
1 2 3 4 5 | # Open the text file in read mode using the with statement with open('myfile.txt', 'r') as file: # Read the contents of the file into a list of strings lines = file.readlines() print(lines) |
Output:['Hi\n', "I'm Rahul\n", 'Welcome you on tecadmin.net\n']
In this example, we use the `with`
statement to open the text file in read mode (the ‘r’ parameter indicates that we want to read the file). The `with`
statement automatically closes the file after the block of code within the `with`
statement has been executed. Within the `with`
statement, we use the `.readlines()`
method to read the contents of the file into a list of strings called lines.
Conclusion
In this article, we have covered four different methods for reading a text file in Python: using the `open()`
function and `.read()`
method, using the `open()`
function and `.readlines()`
method, using the `with`
statement and `.read()`
method, and using the `with`
statement and `.readlines()`
method. Each of these methods has its own advantages and disadvantages, and the best method to use will depend on your specific needs.