我正在編寫和測試XPsp3 w/python 2.7上的代碼。我在2003服務器w/python 2.7上運行代碼。我的目錄結構是這個樣子蟒蛇列出基於平臺的不同順序dirs
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
每個目錄裏面有,將有「IWPCPatch」作爲文件名的一部分,一個或多個文件。
裏面其中的一個文件(每個目錄),將有行「IWPCPatchFinal_a.wsf」
我做的是
1)在所有迪爾斯os.walk在d:\ ssptemp
2)發現在文件名
3)檢查文件的內容爲 'IWPCPatchFinal_a.wsf'
4)如有合作與 'IWPCPatch' 所有文件ntents是真的我將該文件的路徑添加到列表。
我的問題是,在我的XP機器上工作正常。如果我列出列表的結果,我會按照上面列出的順序獲得多個項目。
當我將它移動到server 2003機器時,我以不同的順序得到相同的內容。它來自ssp10-X,然後是ssp9-X。這導致我在程序中的不同區域出現問題。
我可以從我的輸出中看到,它以錯誤的順序開始os.walk,但我不知道爲什麼發生這種情況。
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
他說什麼。不要依賴操作系統來做正確的事情(特別是對於Windows而言,每個版本之間的差異很大)。在迭代它之前,自行對數據進行排序。 – jathanism 2011-04-14 18:06:45
多數民衆贊成我以爲你會說:( – ccwhite1 2011-04-14 18:17:28
如果我做了一個list.sort()在XP它把他們在2003年相同的順序。我能做的是拉數字在目錄名稱和創建列表爲[path,X,Y](即[,10,1])。然後,我可以在X,Y列上進行某種排序。但是如何進行2列排序? –
ccwhite1
2011-04-14 18:25:50