python - 如何在 Python 中获取绝对文件路径

标签 python path relative-path absolute-path

给定一个路径,例如 "mydir/myfile.txt",如何在 Python 中找到文件的绝对路径?例如。在 Windows 上,我可能会得到:

"C:/example/cwd/mydir/myfile.txt"

最佳答案

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

如果它已经是绝对路径也可以:

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

关于python - 如何在 Python 中获取绝对文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51520/

相关文章:

python - 终端路径不是实际位置

python - K 表示置换簇

python - Vincent 图没有显示,没有收到错误

Python字典键错误仅在一种特定情况下发生

Java Runtime.getRuntime().exec() 似乎正在覆盖 $PATH

.net - 获取相对于当前地址的路径的最佳方法是什么?

python - 当我没有使用 @staticmethod 装饰器时,为什么我的减法函数在静态上下文中工作?

javascript - 在子 iframe 中使用 window.location 的相对 url

html - src 属性仅适用于相对路径

javascript - 将绝对路径转换为相对路径