Re: Searching for a file

"Peter J. Holzer" <[email protected]>
Newsgroups gmane.comp.python.general
Message-ID <[email protected]>
On 2025-05-24 17:18:11 -0600, Mats Wichmann wrote:
> 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.
> 
> They don't give you "search in a list of directories" intrinsically, but
> that's simple loop, bailing out on a match, no?

But they all read directories. For Rob's purpose this isn't necessary.
He just needs to test a fixed number of locations. Reading even one
directory (muss less recursively scanning a whole tree like os.walk
does) is just pointless extra work.

        hjp

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | [email protected]         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmg1nEoACgkQ8g5IURL+
KF2gCg/9GqihNRYhEmlaliQd83qp3zvTuq/qrNQMmwOW3y3Z0OoZvJg9neIiDpdG
sHHn6gvtxRGLFiSLz2lJ+6IuBGojhwxVARC8Z7vRNOlrMfEIzQgUgSRd6H79MZR2
d3b5L2p91QN1FNIFLNJuBJIv/XeAwWJnbRoguCrXAgeME3jrXmfQm5iKMP4UWdMT
Zs+8FrRQ30XE7J/2F6PDZbqoJu+n8UNUknJUyebAANxNPMj4L5xJXqclQ1NWk3La
Fn6kxBVcWB9i7nhHF3Bk759dvhDFQ3fjfLY9RqRVT3fZLx+zJSmVmq9USIn2P92/
dw3v+vHT+jc0MXdGFCMnlVzOAQrdDWcvqpK/uIQixoji8cHly35bxdMiBpg0ZDtt
eAwRvPx2K0cdPbWWBclJ4CY6z78+AgzBSxVcLomC4L4/DNf/xB4+deJ6vXEN+7QT
Xyveu5/NgEx6VqcmTcV+ABIpnwphixR7QBYiL/vbQgZ3HWYllbx7SjuzjSJA+Fdk
7kN8YU0OPJ5Z6xG8btI1oaKty0J0JnbZGy+QHPKbWHLXUFF+jlad2R94pWS2S3xi
s2SJ7efnlDPbGTLsSgGcvGjLP6BLJuQ3m1PGhqLBU8Az1LkZIVJoAU+zSsWLDJXS
kRlpXIVWyZoFHmd5CWRsh8/+kYsSIGwVRLmGGz3dVLs5eahSbjs=
=V7E/
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.