To read a file line-by-line into a list in Python, you can use the following methods. Each approach handles newline characters (\n
) differently, so choose based on your needs:
Method 1: Using readlines()
Reads all lines into a list including newline characters:
with open('file.txt', 'r') as file:
lines = file.readlines() # Includes '\n' at the end of each line
To remove newline characters:
with open('file.txt', 'r') as file:
lines = [line.rstrip('\n') for line in file] # Removes only the trailing '\n'
# OR
lines = [line.strip() for line in file] # Removes leading/trailing whitespace and '\n'
Method 2: Iterate Directly Over the File Object
Memory-efficient and clean:
with open('file.txt', 'r') as file:
lines = []
for line in file:
lines.append(line.rstrip('\n')) # Or line.strip()
Method 3: Use read()
+ splitlines()
Read the entire file as a string and split into lines without newlines:
with open('file.txt', 'r') as file:
content = file.read()
lines = content.splitlines() # Automatically removes '\n' and other line endings
Key Differences
Method | Includes \n ? | Handles Different Line Endings? | Memory Efficiency |
---|---|---|---|
file.readlines() | Yes | ❌ (Retains \n ) | ❌ (Reads all at once) |
read() + splitlines() | No | ✅ (Handles \n , \r\n , etc.) | ❌ (Reads all at once) |
Iterating line-by-line | Configurable | ✅ | ✅ (Streams lines) |
Examples
Sample File (file.txt
):
Hello
World
Python
Output Comparison
- Using
readlines()
:
['Hello\n', 'World\n', '\n', 'Python']
- Using
rstrip('\n')
:
['Hello', 'World', '', 'Python']
- Using
splitlines()
:
['Hello', 'World', '', 'Python']
Edge Cases
- Empty Lines: Preserved as empty strings (
''
). - Trailing Newlines: Use
rstrip('\n')
to avoid stripping other whitespace. - Large Files: For huge files, iterate line-by-line (Method 2) to avoid loading the entire file into memory.
Final Recommendation
Use splitlines()
for simplicity and automatic handling of line endings:
with open('file.txt', 'r') as file:
lines = file.read().splitlines()
Or for memory efficiency with large files:
with open('file.txt', 'r') as file:
lines = [line.rstrip('\n') for line in file]