Home arrow Tricks, Tips, and Help arrow Advanced Tips arrow Python path walk list directories and files
Python path walk list directories and files Print E-mail
I needed to crawl a directory structure and get a list of all the files with their absolute location, I think the easiest way to do this is with os.path.walk. The syntax is a little confusing at first, but it works fine and I end up with the list of all files with their folders from whatever location I start at.



def check(arg,dirname,fnames): #This is called recursivly by os.path.walk until all paths have been walked
                                                             #Also it gets the current dir and all the filenames in the dir each time
      for file in fnames: #Then I iterate throught all the filenames it gets
         arg.append(os.path.join(dirname,file)) #and append them with their directory to the overall list
 
os.path.walk("./",check, arglist) #Call this and all the recursion starts
 

So os.path.walk calls itself on every folder pushing the folder and its list of files to my  function check. Check then adds the full filename to arg, which is passed recursivly throught the walk.

print arg

Will list out all the files and directories.
 
< Prev

Jibwa Work Samples

Contact Jibwa LLC

Under Construction

Jibwa.com is under construction. Watch out for broken links, missing pages, potholes and bulldozers. We apologize for the temporary inconvenience - Jibwa.com Staff

News and Updates

Flex 4 Pediatrics One

Recently Jibwa LCC published demonstration videos and a new website design for Pediatrics One Clinical Management software built on Flex Flash Builder...
Read More ...

Eclectic Flea Simple Business Site

Using hand written materials and some photos we managed to create a simple site for Tucson's artsy thrift store. The Eclectic Flea ...
Read More ...

Flash Builder 2 Release changes from beta

I am moving from (flex) Flash Builder Beta 2 to Flash Builder Release Stable and keeping notes on changes I've had to make to my code. 1.) mx names...
Read More ...

Radiology Gallery

Jibwa and Tripwirearts have built and  launched a new website with Dr Benjamin Strong. radiologygallery.com for radiology continuing medical educ...
Read More ...