String resolving test

I am new to python with a problem that is hard to handle.

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

eg. type string /some/path_to/directory_1/and_to/directory_2
with real path:/some/path_to/directory 1/and_to/directory 2

note that the real path may contain BOTH spaces and underscores.

How can i pass it os.path.exists()

thanks Alessandro

+5
source share
1 answer

Use glob , but replacing each underscore with a range [ _]:

import glob
glob.glob('/some/path_to/directory_1/and_to/directory_2'.replace('_', '[ _]'))

, , [. , [ [[].

+5

All Articles