473,388 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 1649
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ADE | last post by:
Hi I was wondering if one of you could help me out I would like for this function to be able to find file names as well instead of printing out every file that ends with a .jpg or .gif extension...
12
by: rbt | last post by:
Not really a Python question... but here goes: Is there a way to read the content of a PDF file and decode it with Python? I'd like to read PDF's, decode them, and then search the data for certain...
4
by: anton | last post by:
Hi, I am googeling some hours now ... still without result. So I have a question: Does somebody know a filemanager: - which looks like Norton Commander/7-Zip Filemanager
0
by: Shane | last post by:
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: ...
1
by: maya | last post by:
let's see if I get better luck here, I posted this question in another ng, but got no responses.. I'm at work, where my machine has .NET Framework running in Windows Server 2003 Standard Ed.. ...
29
by: jaysherby | last post by:
I'm new at Python and I need a little advice. Part of the script I'm trying to write needs to be aware of all the files of a certain extension in the script's path and all sub-directories. Can...
10
by: sandipm | last post by:
Hi, In my application, I have some configurable information which is used by different processes. currently I have stored configration in a conf.py file as name=value pairs, and I am importing...
4
by: haxier | last post by:
Hi all I'm developing an application with some reports and we're looking for advice. This reports should be openoffice.org .odf files, pdf files, and perhaps microsoft word files (.doc, .docx?)...
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.