Tuesday, 20 August 2013

Preferred file line by line read idiom in Python

Preferred file line by line read idiom in Python

I feel like almost every time I read a file in Python, what I want is:
with open("filename") as file_handle:
for line in file_handle:
#do something
Is this truly the preferred idiom? It mildly irritates me that this double
indents all file reading logic. Is there a way to collapse this logic into
one line or one layer?

No comments:

Post a Comment