Home arrow Tricks, Tips, and Help arrow Scripts and Tricks for Professionals 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

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

Doran Photographic Works

A local Tucson photographer was looking for the best way of displaying and selling his photography on the web. We ended up finding the best solution w...
Read More ...

Boyd Energy Site

A energy engineer wanted a place to share his knowledge of energy conservation and other building type technologies. Using Joomla 1.5 we have a site u...
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 ...