searching for files on Windows with Python

Discussion in 'Python' started by =?iso-8859-4?Q?Shane?=, Nov 17, 2005.

  1. I've been giving Google a good workout with no luck. I would like to be able to search a Windows filesystem for filenames, returning a list off absolute paths to the found files, something like:

    def findFiles(filename, pathToSearch):
    ...
    ...
    return foundFileNames

    Is the os module where I should start?

    Thanks,

    Shane
     
    =?iso-8859-4?Q?Shane?=, Nov 17, 2005
    #1
    1. Advertising

  2. Shane schrieb:
    > I've been giving Google a good workout with no luck. I would like to be able to search a Windows filesystem for filenames, returning a list off absolute paths to the found files, something like:
    >
    > def findFiles(filename, pathToSearch):
    > ...
    > ...
    > return foundFileNames
    >
    > Is the os module where I should start?
    > [...]


    Yes, especially the os.walk() function should help you.

    Bye,
    Dennis
     
    Dennis Benzinger, Nov 17, 2005
    #2
    1. Advertising

  3. =?iso-8859-4?Q?Shane?=

    Kent Johnson Guest

    Shane wrote:
    > I've been giving Google a good workout with no luck. I would like to
    > be able to search a Windows filesystem for filenames, returning a
    > list off absolute paths to the found files, something like:>
    > def findFiles(filename, pathToSearch):
    > ...
    > ...
    > return foundFileNames
    >
    > Is the os module where I should start?


    I always use Jason Orendorff's path module for this kind of stuff. It's way easier to use than os.whatever:

    import path
    files = path.path(pathToSearch).walkfiles(filename)

    will give a list of path.path objects in pathToSearch whose names match filename (which is a glob so wildcards are recognized).

    path.path is a subclass of str so the results can be used wherever you want the full path.

    http://www.jorendorff.com/articles/python/path/index.html

    Kent
     
    Kent Johnson, Nov 17, 2005
    #3
  4. =?iso-8859-4?Q?Shane?=

    Peter Hansen Guest

    Kent Johnson wrote:
    > I always use Jason Orendorff's path module for this kind of stuff. It's
    > way easier to use than os.whatever:
    >
    > import path
    > files = path.path(pathToSearch).walkfiles(filename)


    A minor enhancement (IMHO) (though I certainly agree with Kent's
    recommendation here): since there is nothing else of interest in the
    "path" module, it seems to be a fairly common idiom to do "from path
    import path" and skip the doubled "path.path" bit.

    -Peter
     
    Peter Hansen, Nov 18, 2005
    #4
  5. =?iso-8859-4?Q?Shane?=

    Kent Johnson Guest

    Peter Hansen wrote:
    > Kent Johnson wrote:
    >> import path
    >> files = path.path(pathToSearch).walkfiles(filename)

    >
    > A minor enhancement (IMHO) (though I certainly agree with Kent's
    > recommendation here): since there is nothing else of interest in the
    > "path" module, it seems to be a fairly common idiom to do "from path
    > import path" and skip the doubled "path.path" bit.


    Certainly it's your choice. I find most programs using path only reference path.path once, to create a starting path; other paths are created from that using files() or / etc. In this case it is less typing to say

    import path
    basePath = path.path(...)

    instead of

    from path import path
    basePath = path(...)

    from path import path only wins on number of chars if you reference path *three* times.

    YMMV :)

    Kent
     
    Kent Johnson, Nov 19, 2005
    #5
    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. sal achhala

    Searching XML files with DOM

    sal achhala, Mar 1, 2004, in forum: Java
    Replies:
    0
    Views:
    321
    sal achhala
    Mar 1, 2004
  2. hivie

    Searching text files

    hivie, Jul 8, 2003, in forum: C++
    Replies:
    3
    Views:
    539
    John Harrison
    Jul 8, 2003
  3. Fredrik Lundh
    Replies:
    1
    Views:
    334
    Do Re Mi chel La Si Do
    Nov 17, 2005
  4. Replies:
    0
    Views:
    769
  5. stumblng.tumblr
    Replies:
    1
    Views:
    231
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page