I have a directory bar inside a directory foo, with file foo_file.txt in directory foo and file bar_file.txt in
bar
foo
foo_file.txt
bar_file.txt
os.path.relpath() assumes that its arguments are directories.
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'