Python os.walk example
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
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'] ----
Related posts
- How to get the filename and it's parent directory in Python — posted 2011-12-28
- How to remove ^M characters from a file with Python — posted 2011-10-03
- Options for listing the files in a directory with Python — posted 2010-04-19
- Monitoring a filesystem with Python and Pyinotify — posted 2010-04-09
- os.path.relpath() source code for Python 2.5 — posted 2010-03-31
- A hack to copy files between two remote hosts using Python — posted 2010-02-08
Comments
Thanks for your post and example. It helped me.
Many thanks. Really util for me.
This was extremely helpful! Thank you!
Thanks a lot for your example. It really helped!
it helped me greatly
interesting how it returns the windows path with both a '/' and '\'.
Thaks man! exactly the code example i was looking for...
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".
got a good grip
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!
Is it possible to have this method order the files by date rather than by name?
Simple and useful, thanks a lot
Clear, simple, understandable example. Thanks!
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.