2013-09-29 33 views
2

所以我創造了這個文件夾C:試運行下面的代碼片段錯誤使用os.path.walk()正確

這個文件夾我有兩個文件裏面\ TempFiles中 - > nd1.txt,nd2.txt和一個文件夾C:\ TempFiles中\ TEMP2,這裏面我有隻有一個文件nd3.txt

現在,當我執行此代碼: -

import os,file,storage 
database = file.dictionary() 
tools = storage.misc() 
lui = -1       # last used file index 
fileIndex = 1 

def sendWord(wrd, findex):     # where findex is the file index 
global lui 
if findex!=lui: 
    tools.refreshRecentList() 
    lui = findex 
if tools.mustIgnore(wrd)==0 and tools.toRecentList(wrd)==1: 
    database.addWord(wrd,findex)  # else there's no point adding the word to the database, because its either trivial, or has recently been added 

def showPostingsList(): 
    print("\nPOSTING's LIST") 
    database.display() 

def parseFile(nfile, findex): 
    for line in nfile: 
     pl = line.split() 
     for word in pl: 
      sendWord(word.lower(),findex) 

def parseDirectory(dirname): 
    global fileIndex 
    for root,dirs,files in os.walk(dirname): 
     for name in dirs: 
      parseDirectory(os.path.join(root,name)) 
     for filename in files: 
      nf = open(os.path.join(root,filename),'r') 
      parseFile(nf,fileIndex) 
      print(" --> "+ nf.name) 
      fileIndex+=1 
      nf.close() 

def main(): 
    dirname = input("Enter the base directory :-\n") 
    print("\nParsing Files...") 
    parseDirectory(dirname) 
    print("\nPostings List has Been successfully created.\n",database.entries()," word(s) sent to database") 
    choice = "" 
    while choice!='y' and choice!='n': 
     choice = str(input("View List?\n(Y)es\n(N)o\n -> ")).lower() 
     if choice!='y' and choice!='n': 
      print("Invalid Entry. Re-enter\n") 
    if choice=='y': 
     showPostingsList() 

main() 

現在我要遍歷三個文件只有一次每個,和我把打印(文件名)來測試,但顯然我遍歷內部文件夾兩次: -

Enter the base directory :- 
C:\TempFiles 

Parsing Files... 
--> C:\TempFiles\Temp2\nd3.txt 
--> C:\TempFiles\nd1.txt 
--> C:\TempFiles\nd2.txt 
--> C:\TempFiles\Temp2\nd3.txt 

Postings List has Been successfully created. 
34 word(s) sent to database 
View List? 
(Y)es 
(N)o 
-> n 

誰能告訴我如何修改os.path.walk()這樣以避免錯誤 它不是我的輸出是不正確的,但它的穿越過一個整個文件夾的兩倍,這是不是很高效。

回答

0

您的問題不是特定於Python 3中,這是怎麼os.walk()作品 - 迭代已經做遞歸到子文件夾,這樣你就可以拿出你的遞歸調用:

def parseDirectory(dirname): 
    global fileIndex 
    for root,dirs,files in os.walk(dirname): 
     for filename in files: 
      nf = open(os.path.join(root,filename),'r') 
      parseFile(nf,fileIndex) 
      print(" --> "+ nf.name) 
      fileIndex+=1 
      nf.close() 

通過調用parseDirectory()dirs,你開始另一個獨立的步行你唯一的子文件夾。

+0

謝謝,它現在工作完美。 – VivekGhosh