How to get an absolute file path in Python

前端 未结 9 1323
孤城傲影
孤城傲影 2020-11-22 11:53

Given a path such as \"mydir/myfile.txt\", how do I find the file\'s absolute path relative to the current working directory in Python? E.g. on Windows, I might

9条回答
  •  眼角桃花
    2020-11-22 12:27

    >>> import os
    >>> os.path.abspath("mydir/myfile.txt")
    'C:/example/cwd/mydir/myfile.txt'
    

    Also works if it is already an absolute path:

    >>> import os
    >>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
    'C:/example/cwd/mydir/myfile.txt'
    

提交回复
热议问题