os.path.expanduser()
作用:替换路径中的~
用法
>>>os.path.expanduser("~/Build") '/usr/testuser/Build' >>>os.path.exists(os.path.expanduser("~/Build")) True
os.path.expandvars()
作用:替换路径中的$NAME或者${NAME}
用法
>>>os.path.expandvars("$HOME") '/home/tt'举一个在实际项目中用到的例子
def handle(self, **options): if options["filename"]: fileobj = open(expanduser(expandvars(options["filename"])), "w") else: fileobj = self.stdout fileobj = write_nexus( fileobj, options["language_list"], options["meaning_list"], set(["L", "X"]), # exclude options["dialect"], # dialect True, # label cognate sets options["ascertainment_marker"])['fileobj'] fileobj.close()os.path.realpath(path)
作用:返回path的真实路径
注意:同os.path.abspath的区别就是当一个文件为连接文件时,os.path.realpath可以返回链接文件所指向文件的路径,而os.path.realpath则返回链接文件的路径
os.path.abspath(path)
作用:返回path的绝对路径