开发者

permissive equality test on string

开发者 https://www.devze.com 2023-01-17 22:41 出处:网络
I\'m a python newbie with a problem too hard to tackle. I have a string defini开发者_如何学Cng a path, were all the spaces have been converted to underscores.

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

I have a string defini开发者_如何学Cng 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


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

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

Note that this will fail if your path contains the character [. You can fix this by first replacing [ with [[].

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号