trouble with os.path.exists() and wildcards

F

Fernando Rodriguez

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

Erik Max Francis

Fernando said:
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 && (e-mail address removed) && 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
 
E

Eric Williams

Fernando said:
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
 
E

Erik Max Francis

Jeremy said:
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 && (e-mail address removed) && 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
 
B

Bengt Richter

Jeremy said:
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
 
F

Fernando Rodriguez

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. :)
 
J

Jeremy Fincher

Erik Max Francis said:
Jeremy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top