import os
path = r'.'
# r'C:\Documents and Settings\u4cncmgj\Desktop\STAT_R_EUA\results2\residtext'
def extractInfo(fname, fout):
idExtracted = False # indication of not extracted first non-separator line
lastline = '' # init
f = open(fname)
for line in f:
line = line.lstrip() # strip the left whitespaces
if line == '' or line.startswith('==='):
continue # skip the initial non-info lines
if not idExtracted:
fout.write(line) # extract the first info line
idExtracted = True # indicate that it was done
lastline = line # remember the last non-empty
f.close()
fout.write(lastline) # extract the last line
fout.write('\n#################\n')
fout = open('output.txt', 'w')
for barename in os.listdir(path):
name = os.path.join(path, barename)
if not os.path.isdir(name):
extractInfo(name, fout)
fout.close()
|