I have a series of (~950KB) '.txt' output files from a computational chemistry program; Each file will contain the line '****optimisation achieved****' at least once and, depending on the result of the calculation, possibly twice. Does the following code, in which I am trying to find specific lines and print them to a new file, differentiate between the two occurrences of that line?
import os
with open('results.txt', 'a') as writer:
for file in os.listdir('.'):
if file.endswith('.out'):
print(file + ' ', end= ' ', file=writer)
with open(file, 'r') as reader:
for line in reader.readlines():
s=line.strip()
if s=='**** Optimisation achieved ****':
opt='y'
elif s.startswith('Final energy ='):
if opt=='y':
print(s + ' ', end=' ', file=writer)
elif s.startswith('Total number of defects'):
print(s + ' ', end=' ', file=writer)
elif s.startswith('Total charge on defect'):
print(s + ' ', end=' ', file=writer)
elif s.startswith('Defect centre'):
print(s+ ' ', end=' ', file=writer)
elif s.startswith('Fractional'):
if s!='Fractional coordinates of asymmetric unit :':
print(s + ' ', end=' ', file=writer)
elif s.startswith('Final defect energy'):
if opt=='y':
print(s, file=writer)
(Please be patient, I am relatively new to programming)