DoingSomethingWithAllFilesInADirectory

From XPUB & Lens-Based wiki
Revision as of 20:32, 23 September 2010 by Migratebot (talk | contribs) (Created page with "= Doing "something" with all files in a directory = You want to do something repeatedly to all the files (or maybe all files with a certain extension) in a directory, no matte...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Doing "something" with all files in a directory

You want to do something repeatedly to all the files (or maybe all files with a certain extension) in a directory, no matter how "deep" inside folders those files may be.

From the command-line, you can use the wonderful(ly cryptic and difficult) find command, together with the "exec" option:

find . -iname "*.py" -exec echo {} \;


In Python, you can use the os.walk function:

 numbers=off
import os.path
for (dirpath, dirnames, filenames) in os.walk("."):
	for f in filenames:
		#if f.endswith(".py"):
		print os.path.join(dirpath,f)


src: http://docs.python.org/lib/os-file-dir.html#l2h-2715