By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,454 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,454 IT Pros & Developers. It's quick & easy.

Recurse Directories and process files in directory

P: n/a
Hi I need help writing a python script that traverses (recursivly) a
directory and its sub directories and processes all files in the
directory. So at each directory if there are files in it I must build
a list of those files and process them by exectuing a system command
(exec?)

Can some one tell me what methods to use to:
a) Walk the directory tree
b) execute a system command with parameters.

TIA.

Aug 12 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

KraftDiner wrote:
Hi I need help writing a python script that traverses (recursivly) a
directory and its sub directories and processes all files in the
directory. So at each directory if there are files in it I must build
a list of those files and process them by exectuing a system command
(exec?)

Can some one tell me what methods to use to:
a) Walk the directory tree
b) execute a system command with parameters.

TIA.
Hi,
Try os.walk().
Don't remember the syntax right now, should be straightforward. Check
the standard Python docs that come with your Python installation. Or
start python and do:

import os
print os.walk.__doc__

And executing a system comand is os.system(...).

Caveat: there can be security risks with system(). Depends how you use
it and who you let use it with what arguments. Similar to risks with
CGI forms.

HTH
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vasudev Ram
xtopdf - PDF creation/converson toolkit:
http://www.dancingbison.com/products.html
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Aug 12 '06 #2

P: n/a
KraftDiner wrote:
Hi I need help writing a python script that traverses (recursivly) a
directory and its sub directories and processes all files in the
directory. So at each directory if there are files in it I must build
a list of those files and process them by exectuing a system command
(exec?)

Can some one tell me what methods to use to:
a) Walk the directory tree
b) execute a system command with parameters.
You're in luck. Both these are easy.

os.walk iterates through the hierarchy of directories and their contents

os.system executes any command given as a string (which you can build
however you want). Or you might use the newer "subprocess" module.

Gary Herron
TIA.

Aug 12 '06 #3

P: n/a
KraftDiner wrote:
Hi I need help writing a python script that traverses (recursivly) a
directory and its sub directories and processes all files in the
directory.
There's a great os.walk + wrapper in the Python cookbook. I once had an
unrelated problem with it, but check it out and see if it meets your
needs.

http://tinyurl.com/zwbrb

rd

Aug 12 '06 #4

P: n/a
Use os.system to execute a string and os.walk to get a recursive list of files

>>def processdir(curdir,subdirs,files):
.... map(lambda f:os.system('\\cygwin\\bin\\wc -l "%s"' % f), [curdir+os.sep+x for x in files])
....
>>map(lambda x:processdir(*x), os.walk('\\dev\qclient'));
6 \dev\qclient\.classpath
17 \dev\qclient\.project
774 \dev\qclient\bookmarks.html
8 \dev\qclient\kx\qclient\DirTree$1.class
20 \dev\qclient\kx\qclient\DirTree$2.class
17 \dev\qclient\kx\qclient\DirTree$3.class
8 \dev\qclient\kx\qclient\DirTree$4.class
18 \dev\qclient\kx\qclient\DirTree$5.class
9 \dev\qclient\kx\qclient\DirTree$CellRenderer.class
18 \dev\qclient\kx\qclient\DirTree$ServerPopupMenu.cl ass
5 \dev\qclient\kx\qclient\DirTree$ServerTreeNode.cla ss
28 \dev\qclient\kx\qclient\DirTree.class
148 \dev\qclient\kx\qclient\DirTree.java
11 \dev\qclient\kx\qclient\Q$Date.class
4 \dev\qclient\kx\qclient\Q$Dict.class
4 \dev\qclient\kx\qclient\Q$Flip.class
5 \dev\qclient\kx\qclient\Q$KException.class
12 \dev\qclient\kx\qclient\Q$Minute.class
12 \dev\qclient\kx\qclient\Q$Month.class
15 \dev\qclient\kx\qclient\Q$Second.class
18 \dev\qclient\kx\qclient\Q$Time.class
147 \dev\qclient\kx\qclient\Q.class
137 \dev\qclient\kx\qclient\Q.java
8 \dev\qclient\kx\qclient\QClient$1.class
11 \dev\qclient\kx\qclient\QClient$2.class
15 \dev\qclient\kx\qclient\QClient$3.class
9 \dev\qclient\kx\qclient\QClient$4.class
85 \dev\qclient\kx\qclient\QClient.class
185 \dev\qclient\kx\qclient\QClient.java
4 \dev\qclient\kx\qclient\QServer$DefaultListener.cl ass
3 \dev\qclient\kx\qclient\QServer$Listener.class
28 \dev\qclient\kx\qclient\QServer.class
50 \dev\qclient\kx\qclient\QServer.java
59 \dev\qclient\kx\qclient\ServerDialog.class
146 \dev\qclient\kx\qclient\ServerDialog.java
14 \dev\qclient\kx\qclient\ServerDisplay$1.class
18 \dev\qclient\kx\qclient\ServerDisplay$StatusBar.cl ass
19 \dev\qclient\kx\qclient\ServerDisplay.class
78 \dev\qclient\kx\qclient\ServerDisplay.java
[None, None, None]
>>>
KraftDiner wrote:
Hi I need help writing a python script that traverses (recursivly) a
directory and its sub directories and processes all files in the
directory. So at each directory if there are files in it I must build
a list of those files and process them by exectuing a system command
(exec?)

Can some one tell me what methods to use to:
a) Walk the directory tree
b) execute a system command with parameters.

TIA.
Aug 12 '06 #5

P: n/a
On 2006-08-12 09:46:54 -0700, "KraftDiner" <bo*******@yahoo.comsaid:
Hi I need help writing a python script that traverses (recursivly) a
directory and its sub directories and processes all files in the
directory.
In addition to os.walk, I find Jason Orendorff's 'path' module very
helpful and much easier to use. You can get at
<http://www.jorendorff.com/articles/python/path/>. With it, for
instance, processing all the C source files in a directory,
recursively, would look like:

theDir = path('pathToDir')
for theFile in theDir.walkFiles('*.c'):
# Do something

Best,
David

Aug 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.