2014-02-10 74 views
2

我的路徑文件列表,像這樣:提取目錄

paths = [ 
    'A/B/b.py', 
    'A/B/C/c1.py', 
    'A/B/C/c2.py', 
    'M/N/O/o1.py', 
    'M/N/O/o2.py', 
    'M/N/P/p1.py', 
    'M/N/P/p2.py', 
    'M/N/P/R/r2.py' 
] 

我想變換到一個目錄列表如下:

only_dirs = [ 
    'A', 
    'A/B', 
    'A/B/C', 
    'M', 
    'M/N', 
    'M/N/O', 
    'M/N/P', 
    'M/N/P/R', 
] 

這些路徑不存在於磁盤上,它們只是從DB收集的字符串,我想獲取目錄列表以根據路徑中的任何目錄過濾文件。這樣做的最乾淨的方式是什麼?

回答

4
result = set() 
for items in paths: 
    splitted = items.split("/")[:-1] 
    for idx in xrange(1, len(splitted) + 1): 
     result.add("/".join(splitted[:idx])) 

only_dirs = sorted(result) 
print only_dirs 

輸出

['A', 'A/B', 'A/B/C', 'M', 'M/N', 'M/N/O', 'M/N/P', 'M/N/P/R'] 
+1

+1是迄今爲止唯一的答案。 –

+1

我會使用'os.path.dirname()','os.path.split()'讓它跨平臺。 –

+0

@LaurIvan正確,但OP說「這些路徑不存在於磁盤上,它們只是從數據庫中收集的字符串,在問題:) – thefourtheye

0

EDITED ANSWER

爲在thefourtheye評論指出的那樣,我的第一個答案是不正確的。

這裏是一個新的解決問題的方法:

only_dirs = [] 

for path in paths: 
    current = path[:path.rfind('/')] 
    while len(current) > 0: 
     if current not in only_dirs: 
      only_dirs.append(current) 
     current = current[:current.rfind('/')] 

only_dirs.sort() 
print only_dirs 
+0

這產生'[ 'A/B', 'A/B/C', 'A/B/C','M/N/O','M/N/O','M/N/P','M/N/P','M/N/P/R'] '這不是OP的預期。 – thefourtheye

+0

我編輯了我的答案以提供正確的輸出。 – kaspermoerch