开发者

python listing dirs in a different order based upon platform

I am writing and testing code on XPsp3 w/ python 2.7. I am running the code on 2003 server w/ python 2.7. My dir structure will look something like this

d:\ssptemp
d:\ssptemp\ssp9-1
d:\ssptemp\ssp9-2
d:\ssptemp\ssp9-3
d:\ssptemp\ssp9-4
d:\ssptemp\ssp10-1    
d:\ssptemp\ssp10-2
d:\ssptemp\ssp10-3
d:\ssptemp\ssp10-4

Inside each directory there is one or more files that will have "IWPCPatch" as part of the filename.

Inside one of these files (one in each dir), there will be the line 'IWPCPatchFinal_a.wsf'

What I do is

1) os.walk across all dirs under d:\ssptemp

2) find all files with 'IWPCPatch' in the filename

3) check the contents of the file for 'IWPCPatchFinal_a.wsf'

4) If contents is true I add the path of that file to a list.

My problem is that on my XP machine it works fine. If I print out the results of the list I get several items in the order I listed above.

When I move it to the server 2003 machine I get the same contents in a different order. It comes ssp10-X, then ssp9-X. And this is causing me issues with a different area in the program.

I can see from my output that it begins the os.walk in the wrong order, but I don't know why that is occuring.

import os
import fileinput

print "--createChain--"

listOfFiles = []
for path, dirs, files in os.walk('d:\ssptemp'):

    print "parsing dir(s)"
    for file in files:
        newFile = os.path.join(path,file)
        if newFile.find('IWPCPatch') >= 0:
            for line in fileinput.FileInput(newFile):
                if "IWPCPatchFinal_a.wsf" in line:
                    listOfFiles.append(newFile)                            
                    print "Added", newFile开发者_高级运维

for item in listOfFiles:
    print "list item", item


for path, dirs, files in os.walk('d:\ssptemp'):

    # sort dirs and files
    dirs.sort()
    files.sort()

    print "parsing dir(s)"
    # ...


The order of directories within os.walk is not necessarily alphabetical (I think it's actually dependent upon how they're stored within the dirent on the filesystem). It will likely be stable on the same exact directory (on the same filesystem) if you don't change the directory contents (ie, repeated calls will return the same order), but the order is not necessarily alphabetical.

If you want to have an ordered list of filenames you will have to build the list and then sort it yourself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜