ke 28.5.2025 klo 1.45 Thomas Passin (li...@tompassin.net) kirjoitti: > On 5/27/2025 10:41 AM, Roland Mueller via Python-list wrote: > > To get a list of files in a given directory one can use glob.glob and > > The OP had a different problem. He wanted to find a config file of > known name that could be in one of several locations. >
This can be done by replacing the star wildcard the join(dir, '*') with a filename or filename glob pattern. >>> tmp_files = [] >>> for dir in ['/tmp', '/var/tmp']: ... tmp_files += [f for f in glob(join(dir, filename_to_find)) if isfile(f) ] > > > os.path.isfile > > > >>>> from os.path import isfile > >>>> from glob import glob > >>>> files_in_var_tmp = [f for f in glob('/var/tmp/*') if isfile(f) ] > > > > For several directories iterate over the dirs and add the resulting list > of > > files. > > > >>>> tmp_files = [] > >>>> for dir in ['/tmp', '/var/tmp']: > > ... tmp_files += [f for f in glob(dir + '/*') if isfile(f) ] > > > > > > ti 27.5.2025 klo 17.05 Peter J. Holzer (hjp-pyt...@hjp.at) kirjoitti: > > > >> 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. > >> |_|_) | | > >> | | | h...@hjp.at | -- Charles Stross, "Creative writing > >> __/ | http://www.hjp.at/ | challenge!" > >> -- > >> https://mail.python.org/mailman3//lists/python-list.python.org > >> > > -- > https://mail.python.org/mailman3//lists/python-list.python.org > -- https://mail.python.org/mailman3//lists/python-list.python.org