Python os 模块

Python os.getcwd() 函数用于返回当前工作目录

语法

os.getcwd()

参数

无参数。

返回值

返回当前工作目录的绝对路径字符串。

示例

import os

print(os.getcwd())       # 例如 C:\\Users\\me\\project

# 拼接当前目录下的文件路径
config = os.path.join(os.getcwd(), "config.ini")
print(config)

注意事项

getcwd 返回绝对路径。配合 os.path.join 拼接路径最稳。