如何获取当前 Python 脚本的目录

get_script_directory.py
import os
script_directory = os.path.dirname(os.path.realpath(__file__))

这将始终获取运行该代码的 .py 文件的目录,因此如果你在子目录/模块中有 .py 文件,这可能会返回该子目录。

解决方案的原始(部分)来源:StackOverflow


Check out similar posts by category: Python