我有一个目录bar
在目录内foo
, 带文件foo_file.txt
在目录中foo
和文件bar_file.txt
在目录中bar
; i.e.
computer$ ls
foo/
computer$ ls foo/
bar/ foo_file.txt
computer$ ls foo/bar/
bar_file.txt
使用Pythonos.path.relpath http://docs.python.org/2/library/os.path.html#os.path.relpath功能,我期望:
os.path.relpath('foo/bar/bar_file.txt', 'foo/foo_file.txt')
给我:
'bar/bar_file.txt'
然而,它实际上给了我:
'../bar/bar_file.txt'
为什么是这样?有没有一种简单的方法来获得我想要的行为?
编辑:这是在带有 Python 2.7.3 的 Linux 上
os.path.relpath()
假设它的参数是目录。
>>> os.path.join(os.path.relpath(os.path.dirname('foo/bar/bar_file.txt'),
os.path.dirname('foo/foo_file.txt')),
os.path.basename('foo/bar/bar_file.txt'))
'bar/bar_file.txt'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)