Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > check path.exists() with a "converted" path

Reply
Thread Tools

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

 
 
Alessandro
Guest
Posts: n/a
 
      09-27-2010
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
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      09-27-2010
On Mon, Sep 27, 2010 at 2:23 AM, Alessandro <(E-Mail Removed)> 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() ???


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
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
Paul Rudin
Guest
Posts: n/a
 
      09-27-2010
Alessandro <(E-Mail Removed)> 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.





 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      09-27-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
 
Reply With Quote
 
RaphaŽl Plasson
Guest
Posts: n/a
 
      09-27-2010
On Sep 27, 6:23*pm, Alessandro <(E-Mail Removed)> wrote:

>
> 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

 
Reply With Quote
 
Alessandro
Guest
Posts: n/a
 
      09-27-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Server.MapPath() croaking with "failed to map path", but path is legal awbacker@gmail.com ASP .Net 0 05-12-2005 10:50 PM
absolute path versus relative path in JSP Matt Java 3 07-08-2004 08:31 PM
Bizzare: Invalid path to Access mdb, path is good Keith-Earl ASP .Net 1 05-03-2004 03:25 PM
How to check if a path *could* be a legal path? Maciej Sobczak Python 9 04-24-2004 11:12 PM
Tomcat returning 404 error when using additional path (/servlet/myServlet/extra/path/info) Mupota Muchelemba Java 1 02-04-2004 12:43 AM



Advertisments