用python的OS模块怎样生成一个子目录?

使用os.makedirs()函数可以生成一个子目录。该函数会递归地生成多层目录,如果目录已经存在,则会引发FileExistsError异常。

下面是使用os.makedirs()函数生成一个子目录的示例代码:

import os

# 设置父目录路径
parent_dir = '/path/to/parent/dir'

# 设置子目录名称
sub_dir = 'new_dir'

# 使用os.makedirs()函数生成子目录
sub_dir_path = os.path.join(parent_dir, sub_dir)
os.makedirs(sub_dir_path)

代码中的'/path/to/parent/dir'替换为您需要生成子目录的父目录的实际路径即可。

你可能感兴趣的:(python)