python os.makedirs exist
时间: 2023-09-02 12:07:15 浏览: 56
The `os.makedirs()` function in Python creates a new directory with the specified path recursively. If the directory already exists, it raises an `OSError` with the message `FileExistsError: [Errno 17] File exists`.
However, if you use the optional parameter `exist_ok=True`, it will not raise an exception if the directory already exists. Instead, it will silently ignore the creation of the directory and return without any errors.
Here is an example:
```
import os
# create a new directory
os.makedirs('/path/to/new/directory')
# create the same directory again
os.makedirs('/path/to/new/directory') # raises FileExistsError
# create the same directory with exist_ok=True
os.makedirs('/path/to/new/directory', exist_ok=True) # does not raise an exception
```