File Path retrieving problem
steve at holdenweb.com
Wed Feb 25 17:59:32 CET 2009
music24by7 at gmail.com wrote:
> 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
>>> 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"
>>> 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)
>> 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?
>> 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
What, you are saying that
is returning false when the file exists? I find that hard to believe.
Please display some evidence so I can understand this.
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list