Home
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

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

Anthony Ameen Recovery Blog

A good friend of Jibwa LLC, Anthony Ameen, was wounded in Afghanistan and is busy recovering in San Antonio. To aid his family of keeping everyone inf...
Read More ...

Get notified when a class becomes available

Jibwa LLC has worked out a system to notify people when a class becomes available at the University of Arizona. Originally it was written for personal...
Read More ...

Restart Cable Modem and Router

If you have a modem (Cable, DSL, T1, Other), and a router you are probably familiar with the power cycle! The power cycle is three simple steps and ...
Read More ...

Lecture Audio Clip Player

Mark Pirtle Website Russell Public was interested in having Mark's audio samples playable for visitors of the website. In order to do this we had to ...
Read More ...