Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python listing dirs in a different order based upon platform

Tags:

python

os.walk

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
like image 417
ccwhite1 Avatar asked Apr 14 '11 18:04

ccwhite1


1 Answers

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

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

    print "parsing dir(s)"
    # ...
like image 98
jin Avatar answered Nov 15 '22 18:11

jin