4 ms·
...and if you're processing text files line by line, it isn't even necessary to call read() or readlines() on the file, since a file object supports iteration.
by Luyt 16y ago
...and if you're processing text files line by line, it isn't even necessary to call read() or readlines() on the file, since a file object supports iteration. You could say:
for line in open("todo.txt"):
# do something with line
- rbanffy 16y agoYou could also [ do_something_to(line) for line in open('todo.txt') ]
- xiongchiamiov 16y agoAlthough you'd probably want to replace those square brackets with parens to make it a generator, if you're dealing with a largish file.
- rbanffy 16y agoIndeed. Hope it's not a huge file.