2013-02-11 3 views

ответ

0

Благодаря @ATOzTOA
Вы можете использовать os.listdir и os.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

Спасибо, это именно то, что мне нужно :) –

+0

@AhmetSezginDuran Добавил еще один код, см. это тоже. Кроме того, PLS принять этот ответ, он помог ... – pradyunsg

3

Простой список понимание:

[fn for fn in os.listdir(u'.') if os.path.isdir(fn)] 
Смежные вопросы