Recurse Directories and process files in directory

K

KraftDiner

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.
 
V

vasudevram

KraftDiner said:
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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
G

Gary Herron

KraftDiner said:
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
 
B

BartlebyScrivener

KraftDiner said:
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
 
J

Jason Nordwick

Use os.system to execute a string and os.walk to get a recursive list of files

.... map(lambda f:eek:s.system('\\cygwin\\bin\\wc -l "%s"' % f), [curdir+os.sep+x for x in files])
....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.class
5 \dev\qclient\kx\qclient\DirTree$ServerTreeNode.class
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.class
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.class
19 \dev\qclient\kx\qclient\ServerDisplay.class
78 \dev\qclient\kx\qclient\ServerDisplay.java
[None, None, None]
 
D

David Lewis

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top