2011-10-16 13 views

risposta

41
import os.path 
dirs = [d for d in os.listdir('Tools') if os.path.isdir(os.path.join('Tools', d))] 
+1

Oppure rendi un gen expr! –

+5

Oppure 'next (os.walk ('Strumenti')) [1]'. – eryksun

+0

Anche quelle strade funzioneranno! –

16

Per stampare solo le cartelle

print os.walk(DIR_PATH).next()[1] 

per stampare solo i file

print os.walk(DIR_PATH).next()[2] 
+0

Ottengo AttributeError: l'oggetto 'generator' non ha attributo 'next' – sparrow

+0

Funziona perfettamente per me. (Python 2.7, Ubuntu 16, Anaconda env) – rkmalaiya

+3

Python 3 (per cartelle): print (next (os.walk (DIR_PATH)) [1]) –

3

Un altro metodo:

dirs = [entry.path for entry in os.scandir('Tools') if entry.is_dir()] 
Problemi correlati