Examples
- How to Create Directory in Python? New and If Not Exists
- How to Change Directory in Python? Change Current Working Directory
- How to Get Current Working Directory in Python?
- How to Remove Directory in Python? (Empty & With Files)
- How to Copy Directory in Python? Program With Examples
- How to Open Directory in Python? Program & Example
- How to Read All Files in Directory in Python? (All Types of Files)
- How to Copy File in Python From One Directory to Another? (With and Without shutil)
- Python File Append Program (Append to File)
- How to Read Text File in Python Line by Line? (3 Ways)
- How to Get File Name From File Path in Python?
- Get Number of Lines in File in Python (Line Count)
- How to Get Size of File in Python? (File Size Program)
How to Read Text File in Python Line by Line? (3 Ways)
Reading a file line by line in Python is a common task when working with text files. It allows you to process large files efficiently without loading the entire content into memory at once. Here, we will learn how to read a file line by line in Python, enabling you to handle files of any size with ease.
Python provides simple and intuitive methods for file handling, making it straightforward to read text files line by line. By using these methods, you can iterate over the contents of a file and process each line individually. This approach is particularly useful when dealing with log files, data processing, and text analysis tasks.
Read Text File in Python Line by Line
Here's a program that reads a text file line by line in Python:
Code
def read_file_line_by_line(file_path):
try:
with open(file_path, 'r') as file:
for line in file:
print(line.strip())
except IOError as e:
print(f"Unable to read file. {e}")
# Example usage
file_path = "/path/to/file.txt"
read_file_line_by_line(file_path)
Output
Depends on file
Explanation
The read_file_line_by_line() function takes the file path as an argument. It uses the open() function with the 'r' mode to open the file in read mode.
Then, it iterates over each line in the file using a for loop, printing each line after stripping any leading or trailing whitespace characters.
If the file cannot be read due to an IOError, the exception is caught, and an error message is printed.
To use this program, replace "/path/to/file.txt" with the actual path to your file. The program will then read and print each line of the file, allowing you to process the contents line by line.
Read Text File Line by Line Without Newline
Here's a Python program to read a text file line by line and removes the newline character:
Code
with open('file.txt', 'r') as file:
for line in file:
line = line.rstrip('\n') # Remove the newline character
print(line) # Or perform any desired operation on the line
Output
Depends on file
Explanation
In this example, replace 'file.txt' with the path to your text file. The program opens the file using a with statement, which ensures that the file is properly closed after reading.
It then iterates over each line in the file using a for loop. The rstrip('\n') function is used to remove the newline character ('\n') from the end of each line. You can replace the print(line) statement with any operation you want to perform on each line of the file.
Read File Line by Line Without Loading into Memory
If you want to read a text file line by line without loading it into memory all at once, you can use the readline() function.
Code
with open('file.txt', 'r') as file:
line = file.readline()
while line:
line = line.rstrip('\n') # Remove the newline character
print(line) # Or perform any desired operation on the line
line = file.readline()
Output
Depends on file
Explanation
In this example, replace 'file.txt' with the path to your text file.
The program opens the file using a with statement to ensure proper handling of the file. It then uses the readline() function to read each line of the file individually.
The rstrip('\n') function removes the newline character ('\n') from the end of each line. You can replace the print(line) statement with any operation you want to perform on each line of the file. The readline() function is called again within the while loop to read the next line until the end of the file is reached.