I have a file as follows:
cat file.txt
# unimportant comment
# unimportant comment
# unimportant comment
# important line
blah blah blah
blah blah blah
# insignificant comment
# significant comment
xyz
xyz
I would like to print the lines that start with '#'
only if the following line is not commented.
I am hoping to extract the following two lines:
# important line
# significant comment
I tried the following and it does not work:
with open("file.txt","r") as fp:
for line in fp:
if line[0] == '#':
pos = fp.tell()
previous_line_comment = True
elif line[0] != '#' and previous_line_comment:
fp.seek(pos)
print(fp.readline())
previous_line_commented = False
else:
fp.readline()