2024-04-25 09:25:52 发布
网友
我正在尝试获取当前运行文件的目录路径。例如,如果我运行以下文件:X/foo.py和Y/bar.py,并且我正在运行python3 foo.py和foo.py导入bar.py,并且在bar.py中我得到父目录,我希望它是Y而不是X。我如何做到这一点?目前,我用path = pathlib.Path(__file__).parent.resolve()得到了X
X/foo.py
Y/bar.py
python3 foo.py
foo.py
bar.py
path = pathlib.Path(__file__).parent.resolve()
这里已经回答了这个问题: Find the current directory and file's directory
您需要执行以下操作:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
这将返回当前py文件的绝对路径
这里已经回答了这个问题: Find the current directory and file's directory
您需要执行以下操作:
这将返回当前py文件的绝对路径
相关问题 更多 >
编程相关推荐