import os import shutil def fileParser(filename):     tempFile = "M_temp.tmp"     parseNextLine = 0     toParse = [ "CENTER", "LABEL", "NCOMP", "NPRIM", "NPTR" ]     #toParse = [ "CENTER", "LABEL" ]     buf = []     f = open(filename, 'r')     w = open(tempFile, 'w')     while True:         lines = f.readlines(100)         if not lines: break         for l in lines:             #########################################             #########################################             #l =l.rstrip()             if l:                 ll=l.split()                 if not ll[0][0].isdigit() and not ll[0][0] == '-':                     if parseNextLine:                         parseNextLine = 0                         w.write('   '.join(buf[:15]) + '\n')                         del buf[:15]                         while buf:                             w.write('   '.join(buf[:13]) + '\n')                             del buf[:13]                 if ll[0] in toParse or parseNextLine:                     parseNextLine = 1                     buf = buf + ll                 else:                     w.write(l)             ########################################             ########################################     f.close()     w.close()     os.remove(filename)     shutil.copyfile(tempFile, filename)     os.remove(tempFile) def run():     for file in os.listdir("./"):         if file.endswith(".47"):             fileParser(file) if __name__ == '__main__':     run()