Almighty Bus Error

Blog about computer science, code snippets and tips.

GitHub repository with examples here.
Currently a Computer Engineering student at FCT/UNL.
April 17, 2009 at 8:26pm
Tags: Snippet  Python 

Comments (View)

Snippet.Py: Showing all the filenames in a directory

The following Python code prints the name of the chosen directory and all the files in it and then proceeds to print all the files in the other nested directories one nested directory at a time. This method is recursive for nested directories.

def print_dir(loc):
    print loc + "\n"
    for root, dirs, files in os.walk(loc):
        for f in files:
           print f + "\n"
        for d in dirs:
           print_dir(os.path.join(loc, d))
    return

Notes