madCode madCode - 1 year ago 89
Python Question

Relative and Absolute paths of all files

How do I get the absolute paths of all the files in a directory, that could have many sub-folders, in Python?
I know the os.walk gives me a list of directories and files, recursively in a list.
But that doesn't seem to trigger a way, to get what I want.

Answer Source

os.path.abspath makes sure a path is absolute. Use the following helper function:

import os

def absoluteFilePaths(directory):
   for dirpath,_,filenames in os.walk(directory):
       for f in filenames:
           yield os.path.abspath(os.path.join(dirpath, f))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download