You can do:
with open(filename) as f: for line in f: PyparsingGrammar.parseString(line)
Using the with keyword automatically closes the file as soon as you are done and gives you a pen to work with.
for x in something: do_something
- a standard way to iterate through it (material that can be repeated, for example: list, tuple, dictionary in Python.
I forgot to mention, but I suppose you understood this:
when you open a file in Python with with open(filename) as f , you get list , where each line in the list is an element. That is why you can consider f as an iterator.
source share