2013-05-28 14 views
0

我需要os.walk從我的父路徑(tutu),所有子文件夾。對於每一個,每個最深的子文件夾都有我需要用我的代碼處理的文件。對於具有文件的所有最深的文件夾,文件'佈局'是相同的:一個文件* .adf.txt,一個文件* .idf.txt,一個文件* .sdrf.txt和一個或多個文件* .dat。如圖所示。 enter image description here 我的問題是,我不知道如何使用os模塊從我的父文件夾到所有子文件夾的順序迭代。我需要一個函數,對於os.walk中的當前子文件夾,如果該子文件夾爲空,則繼續該子文件夾內的子子文件夾(如果存在)。如果存在,那麼驗證該文件佈局是否存在(這是沒有問題的......),如果是,則應用代碼(沒問題)。如果沒有,並且如果該文件夾沒有更多的子文件夾,則返回到父文件夾並將os.walk移動到下一個子文件夾,並且將所有子文件夾移動到我的父文件夾(tutu)中。要恢復,我需要像下面(Python編寫/虛代碼混合動力)的一些功能:需要'如果os.havefiles'像python中的子文件夾搜索功能

for all folders in tutu: 
    if os.havefiles in os.walk(current_path):#the 'havefiles' don´t exist, i think... 
     for filename in os.walk(current_path): 
      if 'adf' in filename: 
       etc... 
       #my code 
    elif: 
     while true: 
      go deep 
    else: 
     os.chdir(parent_folder) 

你認爲最好的定義在我的代碼來調用來完成這項工作?

這是我試圖使用的代碼,而無需sucess,當然:

import csv 
import os 
import fnmatch 

abs_path=os.path.abspath('.') 
for dirname, subdirs, filenames in os.walk('.'): 
    # print path to all subdirectories first. 
    for subdirname in subdirs: 
     print os.path.join(dirname, subdirname), 'os.path.join(dirname, subdirname)' 
     current_path= os.path.join(dirname, subdirname) 
     os.chdir(current_path) 
     for filename in os.walk(current_path): 
      print filename, 'f in os.walk' 
      if os.path.isdir(filename)==True: 
       break 
      elif os.path.isfile(filename)==True: 
       print filename, 'file' 
     #code here 

在此先感謝...

+0

這沒什麼意義。如果子文件夾爲空,那麼如何遞歸到其子文件夾中? – abarnert

+0

是的。你的更正是真實的。我的意思是'如果文件夾只有文件夾。謝謝 – BioInfoPT

回答

0

我需要一個功能,當前os.walk中的子文件夾,如果該子文件夾爲空,則繼續該子文件夾內的子子文件夾(如果存在)。

這沒有任何意義。如果文件夾爲空,則它沒有任何子文件夾。

也許你的意思是,如果它有沒有普通文件,然後遞歸到它的子文件夾,但是如果它有任何,不要遞歸,而是檢查佈局?

爲了做到這一點,你需要的是這樣的:

for dirname, subdirs, filenames in os.walk('.'): 
    if filenames: 
     # can't use os.path.splitext, because that will give us .txt instead of .adf.txt 
     extensions = collections.Counter(filename.partition('.')[-1] 
             for filename in filenames) 
     if (extensions['.adf.txt'] == 1 and extensions['.idf.txt'] == 1 and 
      extensions['.sdrf.txt'] == 1 and extensions['.dat'] >= 1 and 
      len(extensions) == 4): 
      # got a match, do what you want 

     # Whether this is a match or not, prune the walk. 
     del subdirs[:] 

我假設在這裏,你只需要找到具有完全指定的文件目錄,並沒有其他人。要刪除最後一個限制,只需刪除len(extensions) == 4部分。

沒有必要明確地重複subdirs或任何其他內容,或者從os.walk內部遞歸調用os.walkwalk的整點是它已經遞歸地訪問它找到的每個子目錄,除非你明確地告訴它不要(通過修剪它給你的列表)。

+0

擊敗了我;) – David

+0

亞斯。你的更正是真實的。我的意思是'如果文件夾只有文件夾。謝謝。 – BioInfoPT

0

os.walk會自動遞歸地「挖倒」,所以你不需要自己遞歸樹。

我想這應該是你的代碼的基本形式:

import csv 
import os 
import fnmatch 

directoriesToMatch = [list here...] 
filenamesToMatch = [list here...] 

abs_path=os.path.abspath('.') 
for dirname, subdirs, filenames in os.walk('.'): 
    if len(set(directoriesToMatch).difference(subdirs))==0:  # all dirs are there 
     if len(set(filenamesToMatch).difference(filenames))==0: # all files are there 
      if <any other filename/directory checking code>: 
       # processing code here ... 

而按照Python文檔,如果出於某種原因不想繼續遞歸,剛剛從子目錄刪除條目: http://docs.python.org/2/library/os.html

如果你不是要檢查是否有你找到你要處理的文件NO子目錄,你也可以改變迪爾斯檢查:

if len(subdirs)==0: # check that this is an empty directory 

我不確定我是否理解這個問題,所以我希望這有助於您!

編輯:

好了,如果你需要檢查有沒有文件,而不是,只需使用:

if len(filenames)==0: 

但正如我上面所說,它很可能是更好的只是尋找特定文件而不是檢查空目錄。