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 8 1650
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
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
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
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
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
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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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
|
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:
...
|
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..
...
|
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...
|
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...
|
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?)...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |