2013-02-11 96 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)] 
相关问题