[Python] Python os.path.relpath behavior

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have a directory bar inside a directory foo, with file foo_file.txt in directory foo and file bar_file.txt in directory bar; i.e.

    computer$ ls
    foo/
    computer$ ls foo/
    bar/ foo_file.txt
    computer$ ls foo/bar/
    bar_file.txt


    Using the python os.path.relpath function, I expect:

    os.path.relpath('foo/bar/bar_file.txt', 'foo/foo_file.txt')


    to give me:

    'bar/bar_file.txt'


    However, it actually gives me:

    '../bar/bar_file.txt'


    Why is this? Is there an easy way to get the behavior I want?

    EDIT: This is on Linux with Python 2.7.3

    Continue reading...

Compartilhe esta Página