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

Paul Rudin paul.nospam at rudin.co.uk
Mon Sep 27 05:35:44 EDT 2010


Alessandro <alexxx.magni at gmail.com> 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.








More information about the Python-list mailing list