In Python 2, can I get the real path of a file object after changing directories? -
in python 2, possible real path of file object after os.chdir()
? consider:
$ python >>> f = file('abc', 'w') >>> f.name 'abc' >>> os import path >>> path.realpath(f.name) '/home/username/src/python/abc' >>> os.chdir('..') >>> path.realpath(f.name) '/home/username/src/abc'
the first call realpath
returns path of file on disk, second 1 not. there way path of disk file after chdir
? ideally, i'd general answer, 1 work if didn't create file object myself.
i don't have actual use case this, i'm curious.
sure
f=file(os.path.join(os.getcwd(),"fname"),"w") print f.name
as long use absolute path when initialize it
as martijn pieters points out
f=file(os.path.abspath('fname'),"w") #or os.path.realpath , etc print f.name
Comments
Post a Comment