469,327 Members | 1,226 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

trouble with os.path.exists() and wildcards

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...
Jul 18 '05 #1
7 23891
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.

--
Erik Max Francis && ma*@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
Jul 18 '05 #2
Fernando Rodriguez wrote:
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...


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...
Jul 18 '05 #3
Erik Max Francis <ma*@alcyone.com> wrote in message news:<3F***************@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?

Jeremy
Jul 18 '05 #4
Jeremy Fincher wrote:
Erik Max Francis <ma*@alcyone.com> wrote in message
news:<3F***************@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?

--
Erik Max Francis && ma*@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
Jul 18 '05 #5
On Mon, 17 Nov 2003 11:09:58 -0800, Erik Max Francis <ma*@alcyone.com> wrote:
Jeremy Fincher wrote:
Erik Max Francis <ma*@alcyone.com> wrote in message
news:<3F***************@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
Jul 18 '05 #6
On 17 Nov 2003 22:17:25 GMT, bo**@oz.net (Bengt Richter) wrote:

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.,


Yes, that exactly what I wanted, and the glob trick works fine. Thanks. :-)
Jul 18 '05 #7
Erik Max Francis <ma*@alcyone.com> wrote in message news:<3F**************@alcyone.com>...
Jeremy Fincher wrote:
Erik Max Francis <ma*@alcyone.com> wrote in message
news:<3F***************@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.


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
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Robert Brewer | last post: by
4 posts views Thread by Pierre Quentel | last post: by
11 posts views Thread by Randy Weber | last post: by
9 posts views Thread by Sheldon Cohen | last post: by
reply views Thread by David Hirschfield | last post: by
Krantor
1 post views Thread by Krantor | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.