Re: Searching for a file
Mats Wichmann <[email protected]>
| Newsgroups | gmane.comp.python.general |
|---|---|
| Message-ID | <[email protected]> |
On 5/23/25 16:05, Rob Cliffe via Python-list wrote: > > > On 23/05/2025 18:55, Mats Wichmann wrote: >> On 5/22/25 21:04, Rob Cliffe via Python-list wrote: >>> It occurs to me that it might be useful if Python provided a function >>> to search for a file with a given name in various directories (much >>> as the import.import_lib function searches for a module in the >>> directories in sys.path). >>> This function would perhaps be best placed in the os.path or os modules. >>> To start the ball rolling, I offer this version: >> consider: os.walk, glob.glob, Path.glob >> >> > I have. None of these are appropriate. > os.walk iterates *recursively* over a *single* directory and its > subdirectories. > pathlib.Path.glob so far as I can make out (I have never used pathlib) > does much the same. > glob.glob (so far as I can make out) does a *wildcard* search for > directories matching a *single* pattern. > My suggestion needs a *non-recursive* search for a *file* in a *list* of > *non-wildcarded* directories. > Best wishes > Rob Cliffe They don't give you "search in a list of directories" intrinsically, but that's simple loop, bailing out on a match, no? pathlib's glob method is more like glob.glob than os.walk - though there's also a walk method in pathlib that's in the same spirit as os.walk. The globs are only recursive if you ask them to be, and if you don't want wildcarding, don't include any wildcard characters in the pattern. -- https://mail.python.org/mailman3//lists/python-list.python.org