Read a File using read(), readline(), and readlines() in Python

In Python, you can read files using three primary methods: read() reads the entire file as a single string, readline() reads one line at a time, and readlines() reads all lines and returns them as a list. These methods allow efficient file reading depending on the use case. Let’s explore each method with examples.


Examples

1. Reading an Entire File Using read()

The read() method reads the entire content of a file as a single string.

main.py

</>
Copy
# Open the file in read mode
with open("sample.txt", "r") as file:
    content = file.read()  # Read the entire file

# Print the file content
print(content)

Explanation:

  • open("sample.txt", "r") opens the file in read mode.
  • file.read() reads the entire file content into the variable content.
  • The with statement ensures the file is closed automatically.
  • The print(content) displays the file contents.

Output (if sample.txt contains the following lines):

2. Reading a Single Line Using readline()

The readline() method reads one line at a time from the file.

main.py

</>
Copy
# Open the file in read mode
with open("sample.txt", "r") as file:
    first_line = file.readline()  # Read the first line

# Print the first line
print("First Line:", first_line)

Explanation:

  • open("sample.txt", "r") opens the file for reading.
  • file.readline() reads only the first line of the file.
  • The result is stored in the variable first_line.
  • The print() statement displays the first line.

Output:

3. Reading All Lines Using readlines()

The readlines() method reads all lines of a file and returns them as a list of strings.

main.py

</>
Copy
# Open the file in read mode
with open("sample.txt", "r") as file:
    lines = file.readlines()  # Read all lines into a list

# Print the list of lines
print("Lines:", lines)

Explanation:

  • open("sample.txt", "r") opens the file.
  • file.readlines() reads all lines into a list, where each line is a string.
  • The variable lines holds this list.
  • The print() function displays the list.

Output:

4. Using a Loop to Read a File Line by Line

Instead of using readline() repeatedly, we can use a loop to read the file line by line.

</>
Copy
# Open the file in read mode
with open("sample.txt", "r") as file:
    for line in file:  # Iterate through each line
        print(line.strip())  # Print the line without extra spaces

Explanation:

  • The file is opened using with open("sample.txt", "r").
  • The for loop iterates through each line of the file.
  • line.strip() removes any leading/trailing whitespace.
  • Each line is printed without extra newline characters.

Output:

Hello, this is a sample file.
It contains multiple lines.
Python file handling is easy.

Conclusion

In this tutorial, we covered different file reading methods:

  1. read(): Reads the entire file at once.
  2. readline(): Reads one line at a time.
  3. readlines(): Reads all lines into a list.
  4. Using a for loop provides an efficient way to read large files line by line.

If you are working with large files, prefer line-by-line reading to avoid memory overload.