Perl file::find module equivalent in Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg Yasko

    Perl file::find module equivalent in Python?

    Hi. Does anyone know if there's an equivalent of Perl's file::find
    module in Python? It traverses a directory. I've googled extensively and
    checked this newsgroup and can't find anything like it for Python.

    I'd be using it in Linux to regularly search for files with a script. Is
    this a good application for Python or does Perl have more functionality
    for quick & simple scripting in Linux?

    Thanks.

    -Greg Yasko

  • Skip Montanaro

    #2
    Re: Perl file::find module equivalent in Python?


    Greg> Hi. Does anyone know if there's an equivalent of Perl's file::find
    Greg> module in Python? It traverses a directory. I've googled
    Greg> extensively and checked this newsgroup and can't find anything
    Greg> like it for Python.

    I've never used file::find, but from your short description ("traverses a
    directory"), I suspect you're looking for os.listdir, os.path.walk or
    perhaps glob.glob:

    listdir(...)
    listdir(path) -> list_of_strings

    Return a list containing the names of the entries in the directory.

    path: path of directory to list

    The list is in arbitrary order. It does not include the special
    entries '.' and '..' even if they are present in the directory.

    walk(top, func, arg)
    Directory tree walk with callback function.

    For each directory in the directory tree rooted at top (including top
    itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
    dirname is the name of the directory, and fnames a list of the names of
    the files and subdirectories in dirname (excluding '.' and '..'). func
    may modify the fnames list in-place (e.g. via del or slice assignment),
    and walk will only recurse into the subdirectories whose names remain in
    fnames; this can be used to implement a filter, or to impose a specific
    order of visiting. No semantics are defined for, or required of, arg,
    beyond that arg is always passed to func. It can be used, e.g., to pass
    a filename pattern, or a mutable object designed to accumulate
    statistics. Passing None for arg is common.

    glob(pathname)
    Return a list of paths matching a pathname pattern.

    The pattern may contain simple shell-style wildcards a la fnmatch.

    Skip

    Comment

    • Greg Yasko

      #3
      Re: Perl file::find module equivalent in Python?

      Skip Montanaro wrote:
      [color=blue]
      > Greg> Hi. Does anyone know if there's an equivalent of Perl's file::find
      > Greg> module in Python? It traverses a directory. I've googled
      > Greg> extensively and checked this newsgroup and can't find anything
      > Greg> like it for Python.
      >
      > I've never used file::find, but from your short description ("traverses a
      > directory"), I suspect you're looking for os.listdir, os.path.walk or
      > perhaps glob.glob:
      >
      > Skip
      >[/color]
      Thanks for answering my question. I knew there had to be some way to do
      it:-)

      Comment

      • Andrew Dalke

        #4
        Re: Perl file::find module equivalent in Python?

        Skip:[color=blue]
        > I've never used file::find, but from your short description ("traverses a
        > directory"), I suspect you're looking for os.listdir, os.path.walk or
        > perhaps glob.glob:[/color]

        There's also in 2.3 os.walk

        Help on function walk in module os:

        walk(top, topdown=True, onerror=None)
        Directory tree generator.

        For each directory in the directory tree rooted at top (including top
        itself, but excluding '.' and '..'), yields a 3-tuple

        dirpath, dirnames, filenames

        dirpath is a string, the path to the directory. dirnames is a list of
        the names of the subdirectories in dirpath (excluding '.' and '..').
        filenames is a list of the names of the non-directory files in dirpath.
        Note that the names in the lists are just names, with no path
        components.
        To get a full path (which begins with top) to a file or directory in
        dirpath, do os.path.join(di rpath, name).

        If optional arg 'topdown' is true or not specified, the triple for a
        directory is generated before the triples for any of its subdirectories
        (directories are generated top down). If topdown is false, the triple
        for a directory is generated after the triples for all of its
        subdirectories (directories are generated bottom up).

        When topdown is true, the caller can modify the dirnames list in-place
        (e.g., via del or slice assignment), and walk will only recurse into the
        subdirectories whose names remain in dirnames; this can be used to prune
        the search, or to impose a specific order of visiting. Modifying
        dirnames when topdown is false is ineffective, since the directories in
        dirnames have already been generated by the time dirnames itself is
        generated.

        By default errors from the os.listdir() call are ignored. If
        optional arg 'onerror' is specified, it should be a function; it
        will be called with one argument, an os.error instance. It can
        report the error to continue with the walk, or raise the exception
        to abort the walk. Note that the filename is available as the
        filename attribute of the exception object.

        Caution: if you pass a relative pathname for top, don't change the
        current working directory between resumptions of walk. walk never
        changes the current directory, and assumes that the client doesn't
        either.

        Example:

        from os.path import join, getsize
        for root, dirs, files in walk('python/Lib/email'):
        print root, "consumes",
        print sum([getsize(join(ro ot, name)) for name in files]),
        print "bytes in", len(files), "non-directory files"
        if 'CVS' in dirs:
        dirs.remove('CV S') # don't visit CVS directories

        Andrew
        dalke@dalkescie ntific.com


        Comment

        Working...