trouble with os.path.exists() and wildcards

Bengt Richter bokr at oz.net
Mon Nov 17 17:17:25 EST 2003


On Mon, 17 Nov 2003 11:09:58 -0800, Erik Max Francis <max at alcyone.com> wrote:

>Jeremy Fincher wrote:
>
>> Erik Max Francis <max at alcyone.com> wrote in message
>> news:<3FB8A5B8.3D359C27 at alcyone.com>...
>>
>> > Fernando Rodriguez wrote:
>> >
>> > > How can I check for the xistence of any file that matches a
>> > > wildcard?
>> > >
>> > > For example: ppis-*.iss
>> > >
>> > > os.path.exists() doesn't expand the wildcard...
>> >
>> > Use glob.glob and then os.path.exists in a loop.
>> 
>> Wouldn't the glob.glob only return files that actually exist?
>
>Sure, but isn't that what he wants?  He wrote, "the [existence] of any
>file that maches a wildcard."  He's obviously talking about existing
>files.
>
>Besides, what else could expanding a wildcard mean except enumerating
>every possible match?

Sure, but then what? Maybe the OP was only interested if _any_ matches existed. E.g.,

    pattern = 'ppis-*.iss'
    if glob.glob(pattern): print 'there is at least one file matching %r'%pattern
    else: print 'no files match the %r pattern'%pattern

might be reasonable to do in some context -- without looking at the actual matches, if any.
Maybe this is what Jeremy had in mind (a lot of mind reading around here ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list