check path.exists() with a "converted" path
Peter Otten
__peter__ at web.de
Mon Sep 27 05:43:49 EDT 2010
Alessandro 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() ???
$ mkdir -p aa{\ ,_}aa/bb{\ ,_}b{\ ,_}/c\ c
$ tree
.
|-- aa aa
| |-- bb b
| | `-- c c
| |-- bb b_
| | `-- c c
| |-- bb_b
| | `-- c c
| `-- bb_b_
| `-- c c
`-- aa_aa
|-- bb b
| `-- c c
|-- bb b_
| `-- c c
|-- bb_b
| `-- c c
`-- bb_b_
`-- c c
18 directories, 0 files
$ python
Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import glob
>>> path = "aa_aa/bb_b_/c_c"
>>> glob.glob(path.replace("_", "[_ ]"))
['aa_aa/bb b_/c c', 'aa_aa/bb_b /c c', 'aa_aa/bb b /c c', 'aa_aa/bb_b_/c c',
'aa aa/bb b_/c c', 'aa aa/bb_b /c c', 'aa aa/bb b /c c', 'aa aa/bb_b_/c c']
I didn't want to throw away my demo just because your question was already
anwered ;)
Peter
More information about the Python-list
mailing list