Python: os.walk() with enumeration

Viewed 1357

In Python, I want to go list all the directories in a root directory, and print out the directory number together with the directory. I then want to print out the files in that directory.

The code would be something like:

for subdir, dirs, files in os.walk(root_dir):
    print "Directory " + str(dir_num) + " = " subdir
    for (file_num, file) in enumrate(files):
        print "File " + str(file_num) + " = " file

But how do can I get a value for dir_num, i.e. the number of the directory in the root directory? I know how to do this to print the file number, using enumerate(), but I'm unsure as to how to apply this to os.walk()...

1 Answers
Related