在Python中,获取当前工作路径通常使用 `os` 模块中的 `getcwd()` 函数。以下是如何使用 `os.getcwd()` 获取当前工作路径的示例代码:
import os
current_path = os.getcwd()
print("当前工作路径:", current_path)
执行上述代码后,它会打印出当前Python脚本所在的工作目录路径。
如果你需要获取当前执行脚本的绝对路径,可以使用 `__file__` 变量,并结合 `os.path.abspath()` 函数:
import os
script_path = os.path.abspath(__file__)
print("当前脚本绝对路径:", script_path)
`__file__` 变量表示当前执行的脚本文件的路径,`os.path.abspath()` 函数会返回该路径的绝对形式。