I want to iterate over each line of an entire file. One way to do this is by reading the entire file, saving it to a list, then going over the line of interest. This method
with open(file_path, 'rU') as f:
for line_terminated in f:
line = line_terminated.rstrip('\n')
...
With universal newline support all text file lines will seem to be terminated with '\n'
, whatever the terminators in the file, '\r'
, '\n'
, or '\r\n'
.
EDIT - To specify universal newline support:
open(file_path, mode='rU')
- required [thanks @Dave]open(file_path, mode='rU')
- optionalopen(file_path, newline=None)
- optionalThe newline
parameter is only supported in Python 3 and defaults to None
. The mode
parameter defaults to 'r'
in all cases. The U
is deprecated in Python 3. In Python 2 on Windows some other mechanism appears to translate \r\n
to \n
.
Docs:
with open(file_path, 'rb') as f:
with line_native_terminated in f:
...
Binary mode can still parse the file into lines with in
. Each line will have whatever terminators it has in the file.
Thanks to @katrielalex's answer, Python's open() doc, and iPython experiments.