Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > searching for files on Windows with Python

Reply
Thread Tools

searching for files on Windows with Python

 
 
=?iso-8859-4?Q?Shane?=
Guest
Posts: n/a
 
      11-17-2005
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
 
Reply With Quote
 
 
 
 
Dennis Benzinger
Guest
Posts: n/a
 
      11-17-2005
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
 
Reply With Quote
 
 
 
 
Kent Johnson
Guest
Posts: n/a
 
      11-17-2005
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/p...ath/index.html

Kent
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      11-18-2005
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
 
Reply With Quote
 
Kent Johnson
Guest
Posts: n/a
 
      11-19-2005
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
 
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
Windows 7, searching for all files containing a text string Matty F NZ Computing 25 12-14-2011 06:16 PM
Windows Explorer, searching for files (Vista) Richard Fangnail Computer Support 4 04-23-2010 10:49 AM
Google search result to be URL-limited when searching site, but notwhen searching Web stumblng.tumblr Javascript 1 02-04-2008 09:01 AM
Searching comp.lang.python/python-list@python.org (was: UTF-8) skip@pobox.com Python 0 03-10-2007 02:50 PM
Re: searching for files on Windows with Python Fredrik Lundh Python 1 11-17-2005 09:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57