Thursday, May 16, 2013

Python: listing all files and directories under a directory



import os

allfiles = list()

# listing all the files in the subdirectories
root = "yourDirectory/"
path = os.path.join(root, "targetdirectory")

for path, subdirs, files in os.walk(root):
    for name in files:
        s = os.path.join(path, name)
        allfiles.append(str(s))


for l in allfiles:
    print l

No comments: