469,643 Members | 1,514 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,643 developers. It's quick & easy.

searching for files on Windows with Python

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
Nov 22 '05 #1
8 1552
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
Nov 22 '05 #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
Nov 22 '05 #3
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
Nov 22 '05 #4
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
Nov 22 '05 #5
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
Nov 22 '05 #6
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
Nov 22 '05 #7
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
Nov 22 '05 #8
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
Nov 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ADE | last post: by
12 posts views Thread by rbt | last post: by
reply views Thread by Shane | last post: by
1 post views Thread by maya | last post: by
29 posts views Thread by jaysherby | last post: by
10 posts views Thread by sandipm | last post: by
4 posts views Thread by haxier | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.