Open a File in Read Mode in Python
To open a file in read mode in Python, use the open() function with the mode set to 'r'. This allows you to access the file’s contents without modifying it. The file must exist; otherwise, an error will be raised.
Examples for Opening File in Read Mode
1. Opening a File and Reading Its Content
We can open a file in read mode using open('filename', 'r') and then use the read() method to get its contents.
In this example, we will consider a file example.txt, read its content, and print the content to output.

main.py
# Open a file in read mode
file = open("example.txt", "r")
# Read the entire file content
content = file.read()
# Print the content
print(content)
# Close the file
file.close()
Explanation:
- The
open("example.txt", "r")function opens the file namedexample.txtin read mode. - The
read()method reads the entire content of the file into the variablecontent. - The
print(content)statement displays the file’s content. - Finally, we close the file using
file.close()to free system resources.
Output:

2. Reading a File Line by Line
Instead of reading the entire file at once, we can read it line by line using the readline() method.
In this example, we will consider a file example.txt, read its content line by line, and print each line to output.

main.py
# Open the file in read mode
file = open("example.txt", "r")
# Read the first line
line1 = file.readline()
print(line1.strip())
# Read the second line
line2 = file.readline()
print(line2.strip())
# Close the file
file.close()
Explanation:
- The
readline()method reads a single line from the file at a time. - We use
strip()to remove any trailing newline characters. - Each call to
readline()moves the file pointer to the next line. - The file is closed after reading the required lines.
Output:

3. Reading All Lines Using a Loop
We can use a for loop to iterate through each line in the file efficiently.
In this example, we will consider a file example.txt, read all the lines in the file, and print each line to output in a loop.

main.py
# Open the file in read mode
file = open("example.txt", "r")
# Loop through each line and print
for line in file:
print(line.strip())
# Close the file
file.close()
Explanation:
- The
forloop automatically iterates over each line in the file. - The
strip()method removes any trailing newline characters. - The file is closed after all lines are read.
Output:

4. Using with open() for Automatic File Handling
The with statement ensures that the file is automatically closed after reading, making the code more efficient.
In this example, we will consider a file example.txt, open it using with statement, and print its content to output.

main.py
# Open the file using 'with' to ensure automatic closing
with open("example.txt", "r") as file:
content = file.read()
# Print the file content
print(content)
Explanation:
- The
with open("example.txt", "r") as filestatement opens the file and ensures it closes automatically. - The
read()method reads the entire content into thecontentvariable. - The file is closed as soon as the
withblock ends.
Output:

Conclusion
In this tutorial, we have covered how to open a file in read mode using different techniques:
open()withread(): Reads the entire file content.readline(): Reads one line at a time.forloop: Iterates over the file line by line.- Using
with open(): Ensures automatic file closure.
