# Python os.makedirs 参考

URL: https://caijiao.org/reference/python/os-makedirs
Source: docs/reference/python/os-makedirs.md
Description: Python os.makedirs 参考，整理标准库调用语法、参数、示例、返回值和常见错误。

Python os.makedirs 参考，整理标准库调用语法、参数、示例、返回值和常见错误。

## 语法

```python
import os

os.makedirs("cache/data", exist_ok=True)
```

## 参数

- `path / name`：文件路径、目录名或环境变量名。
- `exist_ok`：创建目录等操作时用于控制已存在的处理方式。

## 返回值

返回路径、列表、环境变量值或 None；删除、重命名等操作会影响文件系统。

## 示例

```python
import os

os.makedirs("cache/data", exist_ok=True)
```

## 常见错误

- 删除或重命名前没有确认目标路径，容易误操作。
- 环境变量不存在时直接索引会抛出 KeyError。

## 相关内容

- [Python 教程](/python/)
- [Python 问题库](/how-to/python/)
- [JSON Formatter](/tool/json-formatter)
- [Timestamp Converter](/tool/timestamp-converter)
