trouble with os.path.exists() and wildcards

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fernando Rodriguez

    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...
  • Erik Max Francis

    #2
    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

    Comment

    • Eric Williams

      #3
      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.pa th.join(dirname , filespec))

      cya,
      Eric

      --
      ---
      s- should be removed to contact me...

      Comment

      • Jeremy Fincher

        #4
        Re: trouble with os.path.exists( ) and wildcards

        Erik Max Francis <max@alcyone.co m> wrote in message news:<3FB8A5B8. 3D359C27@alcyon e.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

        Comment

        • Erik Max Francis

          #5
          Re: trouble with os.path.exists( ) and wildcards

          Jeremy Fincher wrote:
          [color=blue]
          > Erik Max Francis <max@alcyone.co m> wrote in message
          > news:<3FB8A5B8. 3D359C27@alcyon e.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

          Comment

          • Bengt Richter

            #6
            Re: trouble with os.path.exists( ) and wildcards

            On Mon, 17 Nov 2003 11:09:58 -0800, Erik Max Francis <max@alcyone.co m> wrote:
            [color=blue]
            >Jeremy Fincher wrote:
            >[color=green]
            >> Erik Max Francis <max@alcyone.co m> wrote in message
            >> news:<3FB8A5B8. 3D359C27@alcyon e.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(patte rn): print 'there is at least one file matching %r'%pattern
            else: print 'no files match the %r pattern'%patter n

            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

            Comment

            • Fernando Rodriguez

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


              Comment

              • Jeremy Fincher

                #8
                Re: trouble with os.path.exists( ) and wildcards

                Erik Max Francis <max@alcyone.co m> wrote in message news:<3FB91D06. 5DCB381@alcyone .com>...[color=blue]
                > Jeremy Fincher wrote:
                >[color=green]
                > > Erik Max Francis <max@alcyone.co m> wrote in message
                > > news:<3FB8A5B8. 3D359C27@alcyon e.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

                Comment

                Working...