如何在Python中使用-p选项运行os.mkdir()? [英] How to run os.mkdir() with -p option in Python?

查看:545
本文介绍了如何在Python中使用-p选项运行os.mkdir()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以以下方式运行mkdir命令:

I want to run mkdir command as:

mkdir -p directory_name

在Python中执行此操作的方法是什么?

What's the method to do that in Python?

os.mkdir(directory_name [, -p]) didn't work for me.

推荐答案

您可以尝试以下操作:

# top of the file
import os
import errno

# the actual code
try:
    os.makedirs(directory_name)
except OSError as exc: 
    if exc.errno == errno.EEXIST and os.path.isdir(directory_name):
        pass

这篇关于如何在Python中使用-p选项运行os.mkdir()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆