File Path retrieving problem

music24by7 at gmail.com music24by7 at gmail.com
Wed Feb 25 11:17:40 EST 2009


On Feb 25, 8:57 pm, Steve Holden <st... at holdenweb.com> wrote:
> music24... 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/



Hi Steve,

I have tried your suggested code and also replaced os.path.realpath
with os.path.abspath but still getting the same result.
I want to know is there any workaround for retrieving the filepaths
given only filename

Regards,
Sudhir



More information about the Python-list mailing list