深度阅读

How to create a directory in Python using os.mkdir() or os.makedirs()?

作者
作者
2023年08月22日
更新时间
7.51 分钟
阅读时间
0
阅读量

You can create a directory in Python using the os.mkdir() or os.makedirs() methods. The main difference between these two methods is that os.mkdir() creates a single directory, while os.makedirs() creates a full directory path with all of the intermediate directories.

Here’s an example of how to use os.mkdir() to create a new directory:

import os

# Create a directory
os.mkdir('/path/to/new/directory')

And here’s an example of how to use os.makedirs() to create a new directory path:

import os

# Create a directory path
os.makedirs('/path/to/new/directory')

Both of these methods will create the same directory at the specified location. If the directory already exists, a FileExistsError will be raised.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。