File Path retrieving problem

Steve Holden steve at holdenweb.com
Wed Feb 25 10:57:41 EST 2009


music24by7 at gmail.com wrote:
> Hi all,
> 
> I am new to Python, i have installed python 2.5.4 and it is my
> requirement.
> 
> I need to retrieve the path of filename in python.
> 
> I have found some API's to get this:
> 
> from os.path import realpath
> print realpath("NEWS.txt")  # here NEWS.txt exists and it shows the
> path of the file as C:\Python25\WorkSpace\NEWS.txt
> print realpath("abc.txt") # here abc.txt does not exist but still it
> shows C:\Python25\WorkSpace\abc.txt
> 
> can anybody tell the reason why????
> 
> 
> Now took some safety measures:
> 
> found = lexists(realpath(filename))
>         if found == 0:
>             print "Not Found"
>         else:
>             print realpath(filename)
> 
> i have given the filename as "NEWS.txt" and "abc.txt" but i am always
> getting the output as "Not Found"
> 
> 
> Can anyone please tell me where am i doing wrong????
> 
It seems pretty apparent that lexists() nevert returns a true result.

Why not just

if os.path.exists(filename):
    print os.path.realpath(filename)
else:
    print "Not found"

> also any suggestions to retrieve the filepath from a given filename is
> highly appreciated.
> 
Well, realpath returns the path of the file targeted after any symbolic
links have been evaluated, which may or may not be what you want. Have
you looked at os.path.abspath?

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list