2014-06-10 39 views
0

我想搜索樹中的文件。在窗口中的python-3.4中找到一個文件

我知道樹的文件名和根,但我想找到文件路徑。我使用Python-3.4在Windows 7

我:

#I have a list with C header file name => my_headers_list that look like: 
for part in my_headers_list: 
    print(part) 

#show this 
common.h 
extern.h 
something.h 
else.h 

我也有一棵樹(什麼/是一個文件夾):

Software/ 
    something.c 
    Component1/ 
     extern.c 
     extern.h 
    Component2/ 
     Core/ 
      common.h 
      common.c 
     Config/ 
      common_m.c 
      common_m.h 
    Component3/ 
     Managment/ 
     Core/ 
     Config/ 

等是一個例子,但它確實關閉我的真實情況。

我想:

#a list like the first one, but with the full path of the files 
for part in my_headers_list: 
    print(part) 

#show this 
Software/Component2/Core/common.h 
Software/Component1/extern.h 
Software/Component3/Core/something.h 
Software/Component3/Management/else.h 

我準確的,它應該是通用的,所以我不能讓硬鏈接或硬路徑等

因爲我已經嘗試了一些棘手的腳本的那一刻一些os.listdir等,但它似乎混亂,並不總是工作。

#I try something like this 
dirs = os.listdir("Software/") 
for part in dirs: 
    #don't know how to correctly manage these informations to get what I want. 

你們有辦法做到這一點嗎?

請記住,我不想添加任何lib或插件到我的python。

謝謝

問候,

+1

['os.walk'](https://docs.python.org/2/library/os.html#os.walk) – Kevin

+1

或['glob'] (https://docs.python.org/2/library/glob.html) – Lafexlos

+1

如果你使用的是3.4,你也有['pathlib'](https://docs.python.org/3/library/pathlib。 HTML)如果你喜歡OO風格。 –

回答

0

問題的意見解決。我想用os.walk/glob

謝謝到@kevin @Lafexlos

相關問題