Connecting Tech Pros Worldwide Help | Site Map

trouble with os.path.exists() and wildcards

Fernando Rodriguez
Guest
 
Posts: n/a
#1: Jul 18 '05
Hi,

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...
Erik Max Francis
Guest
 
Posts: n/a
#2: Jul 18 '05

re: trouble with os.path.exists() and wildcards


Fernando Rodriguez wrote:
[color=blue]
> 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...[/color]

Use glob.glob and then os.path.exists in a loop.

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \
\__/ Get married, but never to a man who is home all day.
-- George Bernard Shaw
Eric Williams
Guest
 
Posts: n/a
#3: Jul 18 '05

re: trouble with os.path.exists() and wildcards


Fernando Rodriguez wrote:
[color=blue]
> Hi,
>
> 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...[/color]

have you taken a look at glob.glob?

import glob, os

dirname="."
filespec="ppis-*.iss"

print glob.glob(os.path.join(dirname, filespec))

cya,
Eric

--
---
s- should be removed to contact me...
Jeremy Fincher
Guest
 
Posts: n/a
#4: Jul 18 '05

re: trouble with os.path.exists() and wildcards


Erik Max Francis <max@alcyone.com> wrote in message news:<3FB8A5B8.3D359C27@alcyone.com>...[color=blue]
> Fernando Rodriguez wrote:
>[color=green]
> > 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...[/color]
>
> Use glob.glob and then os.path.exists in a loop.[/color]

Wouldn't the glob.glob only return files that actually exist?

Jeremy
Erik Max Francis
Guest
 
Posts: n/a
#5: Jul 18 '05

re: trouble with os.path.exists() and wildcards


Jeremy Fincher wrote:
[color=blue]
> Erik Max Francis <max@alcyone.com> wrote in message
> news:<3FB8A5B8.3D359C27@alcyone.com>...
>[color=green]
> > Fernando Rodriguez wrote:
> >[color=darkred]
> > > 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...[/color]
> >
> > Use glob.glob and then os.path.exists in a loop.[/color]
>
> Wouldn't the glob.glob only return files that actually exist?[/color]

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?

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \
\__/ Why don't you grow up for crying out loud?
-- Capt. Benjamin "Hawkeye" Pierce
Bengt Richter
Guest
 
Posts: n/a
#6: Jul 18 '05

re: trouble with os.path.exists() and wildcards


On Mon, 17 Nov 2003 11:09:58 -0800, Erik Max Francis <max@alcyone.com> wrote:
[color=blue]
>Jeremy Fincher wrote:
>[color=green]
>> Erik Max Francis <max@alcyone.com> wrote in message
>> news:<3FB8A5B8.3D359C27@alcyone.com>...
>>[color=darkred]
>> > 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.[/color]
>>
>> Wouldn't the glob.glob only return files that actually exist?[/color]
>
>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?[/color]

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
Fernando Rodriguez
Guest
 
Posts: n/a
#7: Jul 18 '05

re: trouble with os.path.exists() and wildcards


On 17 Nov 2003 22:17:25 GMT, bokr@oz.net (Bengt Richter) wrote:

[color=blue][color=green]
>>Besides, what else could expanding a wildcard mean except enumerating
>>every possible match?[/color]
>
>Sure, but then what? Maybe the OP was only interested if _any_ matches existed. E.g.,[/color]

Yes, that exactly what I wanted, and the glob trick works fine. Thanks. :-)


Jeremy Fincher
Guest
 
Posts: n/a
#8: Jul 18 '05

re: trouble with os.path.exists() and wildcards


Erik Max Francis <max@alcyone.com> wrote in message news:<3FB91D06.5DCB381@alcyone.com>...[color=blue]
> Jeremy Fincher wrote:
>[color=green]
> > Erik Max Francis <max@alcyone.com> wrote in message
> > news:<3FB8A5B8.3D359C27@alcyone.com>...
> >[color=darkred]
> > > 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.[/color]
> >
> > Wouldn't the glob.glob only return files that actually exist?[/color]
>
> 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.[/color]

Ah, you misunderstand; my problem was with the "and then
os.path.exists in a loop" part, since, by definition, the files
returned by glob.glob will exist :) I was just trying to kindly point
out that os.path.exists isn't needed for his purposes. I guess I
could've been more clear :)

Jeremy
Closed Thread