Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Recurse Directories and process files in directory

Reply
Thread Tools

Recurse Directories and process files in directory

 
 
KraftDiner
Guest
Posts: n/a
 
      08-12-2006
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.

 
Reply With Quote
 
 
 
 
vasudevram
Guest
Posts: n/a
 
      08-12-2006

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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      08-12-2006
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.
>
>


 
Reply With Quote
 
BartlebyScrivener
Guest
Posts: n/a
 
      08-12-2006
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

 
Reply With Quote
 
Jason Nordwick
Guest
Posts: n/a
 
      08-12-2006
Use os.system to execute a string and os.walk to get a recursive list of files


>>> def processdir(curdir,subdirs,files):

.... map(lambda fs.system('\\cygwin\\bin\\wc -l "%s"' % f), [curdir+os.sep+x for x in files])
....
>>> map(lambda xrocessdir(*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.
>


 
Reply With Quote
 
David Lewis
Guest
Posts: n/a
 
      08-13-2006
On 2006-08-12 09:46:54 -0700, "KraftDiner" <(E-Mail Removed)> 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.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why doesn't this recurse kshudra C Programming 8 10-16-2006 06:08 PM
newb: recurse over elements children and disable all form elements SteveKlett@gmail.com Javascript 4 09-20-2006 07:42 AM
rcase-Recurse with xs:redefine nested groups Cat XML 1 01-27-2005 04:42 AM
Getting all directories/files from current directory and using -d flag for the directories Adam Petrie Perl Misc 8 10-11-2004 01:28 PM
Recurse over a hash without knowing any elements (except top level) Christopher Perl 2 02-16-2004 06:04 AM



Advertisments