check path.exists() with a "converted" path

Discussion in 'Python' started by Alessandro, Sep 27, 2010.

  1. Alessandro

    Alessandro Guest

    Hi, I'm a python newbie with a problem too hard to tackle.

    I have a string defining a path, were all the spaces have been
    converted to underscores.
    How can I find if it corresponds to a real path?

    e.g. a string like '/some/path_to/directory_1/and_to/directory_2'
    with a real path: '/some/path_to/directory 1/and_to/directory 2'

    notice that the real path can contain BOTH spaces and underscores.

    How can I feed it to os.path.exists() ???

    thanks
    alessandro
     
    Alessandro, Sep 27, 2010
    #1
    1. Advertising

  2. Alessandro

    Chris Rebert Guest

    On Mon, Sep 27, 2010 at 2:23 AM, Alessandro <> wrote:
    > Hi, I'm a python newbie with a problem too hard to tackle.
    >
    > I have a string defining a path, were all the spaces have been
    > converted to underscores.
    > How can I find if it corresponds to a real path?
    >
    > e.g. a string like '/some/path_to/directory_1/and_to/directory_2'
    > with a real path: '/some/path_to/directory 1/and_to/directory 2'
    >
    > notice that the real path can contain BOTH spaces and underscores.
    >
    > How can I feed it to os.path.exists() ???


    Use the `glob` module instead: http://docs.python.org/library/glob.html

    from glob import glob
    real_paths = glob(underscored_path.replace('_','[_ ]'))
    #note: it's possible for there to be multiple matches

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Sep 27, 2010
    #2
    1. Advertising

  3. Alessandro

    Paul Rudin Guest

    Alessandro <> writes:

    > Hi, I'm a python newbie with a problem too hard to tackle.
    >
    > I have a string defining a path, were all the spaces have been
    > converted to underscores.
    > How can I find if it corresponds to a real path?
    >
    > e.g. a string like '/some/path_to/directory_1/and_to/directory_2'
    > with a real path: '/some/path_to/directory 1/and_to/directory 2'
    >
    > notice that the real path can contain BOTH spaces and underscores.
    >
    > How can I feed it to os.path.exists() ???


    If you just have the converted string then you can't know what the
    original string is, you have no way of knowing which underscores were
    actually underscores in the original and which where spaces.

    You could generate all possible candidate original strings and check for
    file existence, but in general there are many different original strings
    that will yield the same converted string, possibly more than one of
    those corresponds to a file that exists.
     
    Paul Rudin, Sep 27, 2010
    #3
  4. Alessandro

    Peter Otten Guest

    Alessandro wrote:

    > Hi, I'm a python newbie with a problem too hard to tackle.
    >
    > I have a string defining a path, were all the spaces have been
    > converted to underscores.
    > How can I find if it corresponds to a real path?
    >
    > e.g. a string like '/some/path_to/directory_1/and_to/directory_2'
    > with a real path: '/some/path_to/directory 1/and_to/directory 2'
    >
    > notice that the real path can contain BOTH spaces and underscores.
    >
    > How can I feed it to os.path.exists() ???


    $ mkdir -p aa{\ ,_}aa/bb{\ ,_}b{\ ,_}/c\ c
    $ tree
    ..
    |-- aa aa
    | |-- bb b
    | | `-- c c
    | |-- bb b_
    | | `-- c c
    | |-- bb_b
    | | `-- c c
    | `-- bb_b_
    | `-- c c
    `-- aa_aa
    |-- bb b
    | `-- c c
    |-- bb b_
    | `-- c c
    |-- bb_b
    | `-- c c
    `-- bb_b_
    `-- c c

    18 directories, 0 files
    $ python
    Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
    [GCC 4.4.1] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import glob
    >>> path = "aa_aa/bb_b_/c_c"
    >>> glob.glob(path.replace("_", "[_ ]"))

    ['aa_aa/bb b_/c c', 'aa_aa/bb_b /c c', 'aa_aa/bb b /c c', 'aa_aa/bb_b_/c c',
    'aa aa/bb b_/c c', 'aa aa/bb_b /c c', 'aa aa/bb b /c c', 'aa aa/bb_b_/c c']

    I didn't want to throw away my demo just because your question was already
    anwered ;)

    Peter
     
    Peter Otten, Sep 27, 2010
    #4
  5. On Sep 27, 6:23 pm, Alessandro <> wrote:

    >
    > I have a string defining a path, were all the spaces have been
    > converted to underscores.
    >

    (...)
    >
    > notice that the real path can contain BOTH spaces and underscores.
    >
    > How can I feed it to os.path.exists() ???



    You are losing some information, there is no other choice to test all
    the combinations.
    Something like this should do the trick (/!\ quick and dirty
    solution /!\)

    def testpath(path):
    tmp=path.split("_")
    for i in range(2**(len(tmp)-1)):
    res=""
    for j in range(len(tmp)):
    if j>0:
    if (i & 2**(j-1))==0:
    res += " "
    else:
    res += "_"
    res+=tmp[j]
    if os.path.exists(res):
    return True
    return False

    Raphael
     
    Raphaël Plasson, Sep 27, 2010
    #5
  6. Alessandro

    Alessandro Guest

    people, you're great - best newsgroup I've ever been!

    I think I'll go with the glob suggestion - works like a charm!!!

    thank you...


    alessandro
     
    Alessandro, Sep 27, 2010
    #6
    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. Matt
    Replies:
    2
    Views:
    2,362
    brucie
    Oct 16, 2004
  2. Jason Stacy
    Replies:
    2
    Views:
    4,769
    Roedy Green
    Apr 27, 2008
  3. Muddy Coder

    Can urllib check path exists on server?

    Muddy Coder, Feb 10, 2009, in forum: Python
    Replies:
    1
    Views:
    750
  4. Ulf Meinhardt
    Replies:
    8
    Views:
    6,212
  5. Matt
    Replies:
    3
    Views:
    143
    Grant Wagner
    Sep 17, 2004
Loading...

Share This Page