Чтобы добавить некоторые объяснения:
Чтобы прочитать файл построчно, см. здесь .
Ваша проблема в том, что вы используете несколько вызовов, каждый из которых читает одинили несколько строк из файла, и эта строка пропущена для следующего вызова чтения - см. мои комментарии в коде:
for line in file: // reads the first line and would read again if we came back here before the end of the file, which we do not
if ("Heading A") in line:
for line in file: // reads the second line of the file and would read again if we came back here before the end of the file, which we do not
out = file.readlines()[1:] // reads all remaining lines from the file ( beginning from the third) and drops the first (line three in the file) by indexing [1:]
print(out) // prints out all lines begining with the fourth; after this, the file is at its and and both for loops will be finished
Что вы хотите сделать, это читать построчно и отбрасывать содержимое, содержащее Heading A
:
filename = os.path.abspath(r'C:\x\y\Any.ini') #Using absolute path
file = (open(filename, 'r', encoding='UTF-8'))
for line in file:
if not ("Heading A") in line: print (line)