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

Chris Rebert clp2 at rebertia.com
Mon Sep 27 05:35:24 EDT 2010


On Mon, Sep 27, 2010 at 2:23 AM, Alessandro <alexxx.magni at gmail.com> 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



More information about the Python-list mailing list