In this article, we’ll learn how to read files in Python.
In Python, temporary data that is locally used in a module will be stored in a variable. In large volumes of data, a file is used such as text and CSV files and there are methods in Python to read or write data in those files.
After reading this tutorial, you’ll learn: –
- Reading both text and binary files
- The different modes for reading the file
- All methods for reading a text file such as
read(),readline(), andreadlines() - Read text file line by line
- Read and write files at the same time.
Table of contents
Access Modes for Reading a file
To read the contents of a file, we have to open a file in reading mode. Open a file using the built-in function called open(). In addition to the file name, we need to pass the file mode specifying the purpose of opening the file.
The following are the different modes for reading the file. We will see each one by one.
| File Mode | Definition |
|---|---|
r | The default mode for opening a file to read the contents of a text file. |
r+ | Open a file for both reading and writing. The file pointer will be placed at the beginning of the file. |
rb | Opens the file for reading a file in binary format. The file pointer will be placed at the beginning of the file. |
w+ | Opens a file for both writing as well as reading. The file pointer will be placed in the beginning of the file. For an existing file, the content will be overwritten. |
a+ | Open the file for both the reading and appending. The pointer will be placed at the end of the file and new content will be written after the existing content. |
Steps for Reading a File in Python
To read a file, Please follow these steps:
- Find the path of a file
We can read a file using both relative path and absolute path. The path is the location of the file on the disk.
An absolute path contains the complete directory list required to locate the file.
A relative path contains the current directory and then the file name. - Open file in Read Mode
To open a file Pass file path and access mode to the
open()function. The access mode specifies the operation you wanted to perform on the file, such as reading or writing. For example,ris for reading.
For example,fp= open(r'File_Path', 'r') - Read content from a file.
Once opened, we can read all the text or content of the file using the
read()method. You can also use thereadline()to read file line by line or thereadlines()to read all lines.
For example,content = fp.read() - Close file after completing the read operation
We need to make sure that the file will be closed properly after completing the file operation. Use
fp.close()to close a file.
Example: Read a Text File
The following code shows how to read a text file in Python.
A text file (flatfile) is a kind of computer file that is structured as a sequence of lines of electronic text.
In this example, we are reading all content of a file using the absolute Path.
Consider a file “read_demo.txt.” See the attached file used in the example and an image to show the file’s content for reference.