As discussed in the python file opening, the built-in open()
function is the key function to work with the files. To perform any file-related operations such as creating the file, writing the file, reading the file, etc., you need to use the open() function with different modes such as x
, w
, a
, r
, etc. in python.
First, you need to open the file in read (r
) mode using the built-in open() function to read the file content. After that, you need to use read methods to read the data from files.
In python, we have different methods to read the data from files.
Method | Description |
---|---|
read() | It is useful to read and return the complete text of the file. |
readline() | It is useful to read and return the single-line text of the file. |
readlines() | It is useful to read all lines of text and return a list object. |
Using the read()
method, you can read the complete content of the file in python. Following is the example of reading the data of files using the read()
method in python.
If you observe the above example, we created a file (testfile.txt) and wrote the required text. After that, we used the read()
function to read the content of the file.
It is always good practice to close the file after writing or reading the data from a file. In some cases, the file data will not update until we close the file.
The above example will return the result as shown below.
If you observe the above result, the read()
function has returned the file's whole text.
In the above example, we created a file in the same python location because we didn't mention any file path. If you want to read the data from a file that exists in a different place, you need to specify the file path as shown below.
By default, the read()
function will return the whole text of the file. Instead of reading the entire text of the file, you can specify the number of characters you want in the read()
function.
Following is the example of reading the specified number of characters from the file in python.
When you execute the above example, it will return the result as shown below.
As we have \n
character in the first line of text, we are able to see 6 characters in the result.
Using the read()
function, we can read the whole text or a specified number of characters from the file. If you want to read a single line of text, you need to use the readline()
method.
The readline()
method will return the first line of text from the file. Following is the example of reading a single line of text using the readline()
method in python.
The above example will return the result as shown below.
If you call the readline()
method multiple times, it will return the multiple text lines from the file.
The above example will return the first two lines of text from the file as shown below.
If you want to read all the text lines from the file as a list, you need to use the readlines()
method as shown below.
The above example will return the file content as shown below.
In python, you can iterate through the file content line by line using for loop.
Following is the example of looping through the file content line by line in python.
The above example will return the result as shown below.
You can also use a while loop to iterate through the file content line by line as shown below.
Instead of using for loop and while loop, you can also use file object inbuilt iterator to loop through the file content. Following is the example of reading the file content line by line using the file object inbuilt iterator.
The above example will return the result as shown below.
This is how you can read the data from files using different methods based on your requirements.