2013-02-11 54 views

回答

0

由於@ATOzTOA
您可以使用os.listdiros.path.isfile喜歡這裏:

import os 

path = 'whatever your path is' 

for item in os.listdir(path): 
    if not os.path.isfile(os.path.join(path, item)): 
     print "Folder: ",item 
    else: 
     print "File: ",item 

現在你知道什麼是文件夾和哪些文件。
既然你不想要的文件,你可以簡單的文件夾(路徑或名稱)存儲在列表
對於這一點,做到這一點:

import os 

path = 'whatever your path is' 
folders = [] # list that will contain folders (path+name) 

for item in os.listdir(path): 
    if not os.path.isfile(os.path.join(path, item)): 
     folders.append(os.path.join(path, item)) # os.path.join(path, item) is your folder path 
+0

謝謝,這正是我所需要的:) – 2013-02-11 09:40:38

+0

@AhmetSezginDuran增加了一些代碼,請參閱。另外,請接受這個答案,它幫助... – pradyunsg 2013-02-11 10:09:39

3

簡單列表理解:

[fn for fn in os.listdir(u'.') if os.path.isdir(fn)] 
相關問題