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

Raphaël Plasson rplasson at gmail.com
Mon Sep 27 05:46:08 EDT 2010


On Sep 27, 6:23 pm, Alessandro <alexxx.ma... at gmail.com> 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




More information about the Python-list mailing list