Recurse Directories and process files in directory

Discussion in 'Python' started by KraftDiner, Aug 12, 2006.

  1. KraftDiner

    KraftDiner Guest

    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.
    KraftDiner, Aug 12, 2006
    #1
    1. Advertising

  2. KraftDiner

    vasudevram Guest

    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
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    vasudevram, Aug 12, 2006
    #2
    1. Advertising

  3. KraftDiner

    Gary Herron Guest

    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.
    >
    >
    Gary Herron, Aug 12, 2006
    #3
  4. 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
    BartlebyScrivener, Aug 12, 2006
    #4
  5. 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:eek:s.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.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]
    >>>


    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.
    >
    Jason Nordwick, Aug 13, 2006
    #5
  6. KraftDiner

    David Lewis Guest

    On 2006-08-12 09:46:54 -0700, "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.


    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
    David Lewis, Aug 13, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher
    Replies:
    2
    Views:
    523
    Christopher
    Feb 16, 2004
  2. Cat
    Replies:
    1
    Views:
    540
  3. kshudra

    why doesn't this recurse

    kshudra, Oct 14, 2006, in forum: C Programming
    Replies:
    8
    Views:
    292
    Walter Roberson
    Oct 16, 2006
  4. Adam Petrie
    Replies:
    8
    Views:
    303
    Adam Petrie
    Oct 11, 2004
  5. Replies:
    4
    Views:
    183
Loading...

Share This Page