Saltycrane logo

SaltyCrane Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

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']
----

13 Comments — feed icon Comments feed for this post


#1 Anonymous commented on 2008-03-27:

Thanks for your post and example. It helped me.


#2 boroto commented on 2009-07-11:

Many thanks. Really util for me.


#3 andrew commented on 2009-09-18:

This was extremely helpful! Thank you!


#4 mikeandtherest commented on 2009-11-01:

Thanks a lot for your example. It really helped!


#5 Musthafa commented on 2010-01-27:

it helped me greatly


#6 pedro commented on 2010-10-06:

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


#7 Elad commented on 2010-10-15:

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


#8 DriveByCommenter commented on 2011-02-24:

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 2011-03-25:

got a good grip


#10 Anonymous commented on 2011-10-20:

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 2013-02-16:

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


#12 lokinou commented on 2013-03-13:

Simple and useful, thanks a lot


#13 LeeG commented on 2013-07-31:

Clear, simple, understandable example. Thanks!

Post a comment

Required
Required, but not displayed
Optional

Format using Markdown. (No HTML.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab (and a blank line before and after)
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
Created with Django | Hosted by Linode