Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
31 lines (25 sloc) 917 Bytes
class CommentedFile(object):
"""A file reader class that ignores comments."""
def __init__(self, path, comment_str="#", ignore_empty=True):
:param path:
Path to the text file.
:param comment_str:
Character or other string that indicates a commented line.
Optional. Defaults to '#'.
:param ignore_empty:
If `True`, ignore lines which are empty or only contain whitespace.
self.path = path
self.comment_str = comment_str
self.ignore_empty = ignore_empty
# File handle
self.fh = open(self.path, 'r')
def next(self):
line =
while (line.strip().startswith(self.comment_str)
or (self.ignore_empty and not len(line.strip()))):
line =
return line
def __iter__(self):
return self