for file screening I have following code in two different directories:
import os, re
g=open('results_1.txt', 'w') #Other has 'results_2.txt'
for filename in os.listdir('.'):
    if filename.startswith("f"):
        with open(filename, 'r') as f:
            content =[line.rstrip() for line in f]
        A = filter(lambda x: 'KeyWord_1 :' in x, content)
        B = filter(lambda x: 'KeyWord_2 :' in x, content)
        print >> g,filename,
        for item in A:
            print >> g,item,
        for item in B:
            print >> g,item,
g.close()
Both directories have similar file (to be parsed my script) naming convention. So files look like this: file_1000.txt, file_100.txt, file_101.txt,.....,file_1.txt,......file_9.txt.
I change the script just to change the name of results file. But in one directory the files are sorted from _1 to _1000 and then results file has appropriate order while other does not. Why?
I am sorry this is related to my work and I can give any specifics.
P.S. I tried sorted function and it did not work as I wanted.
 
     
    