How to read a file (or stdin) line by line in Python not waiting for reading entire file
Question:
I want to read and process some large files line by line in python and output results in the terminal. I have gone through How do I read from stdin? and How do I write a unix filter in python?, but I am looking for methods which do not wait till the entire file is read into memory.
I would be using both of these commands:
cat fileName | python myScript1.py
python myScript2.py fileName
Answers:
This is the standard behavior of file objects in Python:
with open("myfile.txt", "r") as myfile:
for line in myfile:
# do something with the current line
or
for line in sys.stdin:
# do something with the current line
if __name__ == '__main__':
while 1:
try:
a=raw_input()
except EOFError:
break
print a
This will read from stdin til EOF.
To read a file using the second method, you can use Tim’s method
i.e.
with open("myfile.txt", "r") as myfile:
for line in myfile:
print line
# do something with the current line
I want to read and process some large files line by line in python and output results in the terminal. I have gone through How do I read from stdin? and How do I write a unix filter in python?, but I am looking for methods which do not wait till the entire file is read into memory.
I would be using both of these commands:
cat fileName | python myScript1.py
python myScript2.py fileName
This is the standard behavior of file objects in Python:
with open("myfile.txt", "r") as myfile:
for line in myfile:
# do something with the current line
or
for line in sys.stdin:
# do something with the current line
if __name__ == '__main__':
while 1:
try:
a=raw_input()
except EOFError:
break
print a
This will read from stdin til EOF.
To read a file using the second method, you can use Tim’s method
i.e.
with open("myfile.txt", "r") as myfile:
for line in myfile:
print line
# do something with the current line