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

A

Alessandro

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() ???

thanks
alessandro
 
C

Chris Rebert

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
 
P

Paul Rudin

Alessandro said:
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.
 
P

Peter Otten

Alessandro said:
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
 
R

Raphaël Plasson

I have a string defining a path, were all the spaces have been
converted to underscores.
(...)

notice that the real path can contain BOTH spaces and underscores.

How can I feed it to os.path.exists() ???


You are losing some information, there is no other choice to test all
the combinations.
Something like this should do the trick (/!\ quick and dirty
solution /!\)

def testpath(path):
tmp=path.split("_")
for i in range(2**(len(tmp)-1)):
res=""
for j in range(len(tmp)):
if j>0:
if (i & 2**(j-1))==0:
res += " "
else:
res += "_"
res+=tmp[j]
if os.path.exists(res):
return True
return False

Raphael
 
A

Alessandro

people, you're great - best newsgroup I've ever been!

I think I'll go with the glob suggestion - works like a charm!!!

thank you...


alessandro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top