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

</>
Copy
# 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:

  1. The open("example.txt", "r") function opens the file named example.txt in read mode.
  2. The read() method reads the entire content of the file into the variable content.
  3. The print(content) statement displays the file’s content.
  4. 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

</>
Copy
# 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:

  1. The readline() method reads a single line from the file at a time.
  2. We use strip() to remove any trailing newline characters.
  3. Each call to readline() moves the file pointer to the next line.
  4. 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

</>
Copy
# 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:

  1. The for loop automatically iterates over each line in the file.
  2. The strip() method removes any trailing newline characters.
  3. 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

</>
Copy
# 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:

  1. The with open("example.txt", "r") as file statement opens the file and ensures it closes automatically.
  2. The read() method reads the entire content into the content variable.
  3. The file is closed as soon as the with block ends.

Output:

Conclusion

In this tutorial, we have covered how to open a file in read mode using different techniques:

  1. open() with read(): Reads the entire file content.
  2. readline(): Reads one line at a time.
  3. for loop: Iterates over the file line by line.
  4. Using with open(): Ensures automatic file closure.