SaltyCrane Blog — Notes on JavaScript and web development

Python os.walk example

Here is a simple os.walk() example which walks your directory tree and returns the path, a list of directories, and a list of files:
import os

path = "c:\\python25"

i = 0
for (path, dirs, files) in os.walk(path):
    print path
    print dirs
    print files
    print "----"
    i += 1
    if i >= 4:
        break
Here are the results:
c:/python25
['DLLs', 'Doc', 'include', 'Lib', 'libs', 'tcl', 'Tools']
['LICENSE.txt', 'NEWS.txt', 'pylupdate4.exe', 'pyrcc4.exe', 'python.exe', 'pythonw.exe', 'pyuic4.bat', 'README.txt', 'temp.py', 'w9xpopen.exe']
----
c:/python25\DLLs
[]
['bz2.pyd', 'py.ico', 'pyc.ico', 'pyexpat.pyd', 'select.pyd', 'sqlite3.dll', 'tcl84.dll', 'tclpip84.dll', 'tk84.dll', 'unicodedata.pyd', 'winsound.pyd', '_bsddb.pyd', '_ctypes.pyd', '_ctypes_test.pyd', '_elementtree.pyd', '_hashlib.pyd', '_msi.pyd', '_socket.pyd', '_sqlite3.pyd', '_ssl.pyd', '_testcapi.pyd', '_tkinter.pyd']
----
c:/python25\Doc
[]
['Python25.chm']
----
c:/python25\include
[]
['abstract.h', 'asdl.h', 'ast.h', 'bitset.h', 'boolobject.h', 'bufferobject.h', 'cellobject.h', 'ceval.h', 'classobject.h', 'cobject.h', 'code.h', 'codecs.h', 'compile.h', 'complexobject.h', 'cStringIO.h', 'datetime.h', 'descrobject.h', 'dictobject.h', 'enumobject.h', 'errcode.h', 'eval.h', 'fileobject.h', 'floatobject.h', 'frameobject.h', 'funcobject.h', 'genobject.h', 'graminit.h', 'grammar.h', 'import.h', 'intobject.h', 'intrcheck.h', 'iterobject.h', 'listobject.h', 'longintrepr.h', 'longobject.h', 'marshal.h', 'metagrammar.h', 'methodobject.h', 'modsupport.h', 'moduleobject.h', 'node.h', 'object.h', 'objimpl.h', 'opcode.h', 'osdefs.h', 'parsetok.h', 'patchlevel.h', 'pgen.h', 'pgenheaders.h', 'pyarena.h', 'pyconfig.h', 'pydebug.h', 'pyerrors.h', 'pyexpat.h', 'pyfpe.h', 'pygetopt.h', 'pymactoolbox.h', 'pymem.h', 'pyport.h', 'pystate.h', 'pystrtod.h', 'Python-ast.h', 'Python.h', 'pythonrun.h', 'pythread.h', 'py_curses.h', 'rangeobject.h', 'setobject.h', 'sliceobject.h', 'stringobject.h', 'structmember.h', 'structseq.h', 'symtable.h', 'sysmodule.h', 'timefuncs.h', 'token.h', 'traceback.h', 'tupleobject.h', 'ucnhash.h', 'unicodeobject.h', 'weakrefobject.h']
----

Comments


#1 Anonymous commented on :

Thanks for your post and example. It helped me.


#2 boroto commented on :

Many thanks. Really util for me.


#3 andrew commented on :

This was extremely helpful! Thank you!


#4 mikeandtherest commented on :

Thanks a lot for your example. It really helped!


#5 Musthafa commented on :

it helped me greatly


#6 pedro commented on :

interesting how it returns the windows path with both a '/' and '\'.


#7 Elad commented on :

Thaks man! exactly the code example i was looking for...


#8 DriveByCommenter commented on :

Thanks, dude. The snippet was easy enough that I got motivated to play around with it a bit, instead of the usual "yeah, I understand it, no need to try it out".


#9 dilip reddy commented on :

got a good grip


#10 Anonymous commented on :

I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! I LOVE YOU!! THANKS A BUNCH!


#11 Saichovsky commented on :

Is it possible to have this method order the files by date rather than by name?


#12 lokinou commented on :

Simple and useful, thanks a lot


#13 LeeG commented on :

Clear, simple, understandable example. Thanks!


#14 Jerry Spicklemire commented on :

Super! Now, how do you display metadata, such as permissions, last change time, last access time, file size, etc. such as:

ls -la

in Linux , Unix,

or

dir /q /a

on Windows?

Preferably in a single command, as above.