trouble with os.path.exists() and wildcards

Discussion in 'Python' started by Fernando Rodriguez, Nov 17, 2003.

  1. 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...
    Fernando Rodriguez, Nov 17, 2003
    #1
    1. Advertising

  2. 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 && && 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
    Erik Max Francis, Nov 17, 2003
    #2
    1. Advertising

  3. 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...
    Eric Williams, Nov 17, 2003
    #3
  4. Erik Max Francis <> wrote in message news:<>...
    > 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
    Jeremy Fincher, Nov 17, 2003
    #4
  5. Jeremy Fincher wrote:

    > Erik Max Francis <> wrote in message
    > news:<>...
    >
    > > 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 && && 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
    Erik Max Francis, Nov 17, 2003
    #5
  6. On Mon, 17 Nov 2003 11:09:58 -0800, Erik Max Francis <> wrote:

    >Jeremy Fincher wrote:
    >
    >> Erik Max Francis <> wrote in message
    >> news:<>...
    >>
    >> > 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
    Bengt Richter, Nov 17, 2003
    #6
  7. On 17 Nov 2003 22:17:25 GMT, (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. :)
    Fernando Rodriguez, Nov 18, 2003
    #7
  8. Erik Max Francis <> wrote in message news:<>...
    > Jeremy Fincher wrote:
    >
    > > Erik Max Francis <> wrote in message
    > > news:<>...
    > >
    > > > 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
    Jeremy Fincher, Nov 18, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas F. O'Connell

    Negative Lookbehind and Wildcards

    Thomas F. O'Connell, Feb 27, 2004, in forum: Perl
    Replies:
    1
    Views:
    709
    Gunnar Hjalmarsson
    Feb 28, 2004
  2. msnews.microsoft.com
    Replies:
    0
    Views:
    1,135
    msnews.microsoft.com
    Jul 18, 2004
  3. Totan
    Replies:
    0
    Views:
    898
    Totan
    Apr 17, 2006
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,089
  5. Alessandro
    Replies:
    5
    Views:
    543
    Alessandro
    Sep 27, 2010
Loading...

Share This Page