Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python for linux中递归创建目录_Python_Linux_Python 2.7 - Fatal编程技术网

在python for linux中递归创建目录

在python for linux中递归创建目录,python,linux,python-2.7,Python,Linux,Python 2.7,我试图使用Python脚本递归地创建目录,但遇到了错误 代码: 错误: 回溯(最近一次呼叫最后一次): 文件“mk.py”,第3行,在 makedirs('/root/test{}'.format(a),exist\u ok=True) TypeError:makedirs()获得意外的关键字参数“exist\u ok” 我使用的是python2.7,上面的选项在此版本中不起作用?如果没有,什么是替代解决方案?是正确的函数,但在Python 2中没有参数存在\u ok。相反,请使用以下命令:

我试图使用Python脚本递归地创建目录,但遇到了错误

代码: 错误:
回溯(最近一次呼叫最后一次):
文件“mk.py”,第3行,在
makedirs('/root/test{}'.format(a),exist\u ok=True)
TypeError:makedirs()获得意外的关键字参数“exist\u ok”
我使用的是python2.7,上面的选项在此版本中不起作用?如果没有,什么是替代解决方案?

是正确的函数,但在Python 2中没有参数
存在\u ok
。相反,请使用以下命令:

请注意,这与Python3中的
exist\u ok
标志的行为不完全匹配。更接近匹配的是:

if os.path.exists(path_to_make):
    if not os.path.isdir(path_to_make):
        raise OSError("Cannot create a file when that file already exists: "
                      "'{}'".format(path_to_make))
else:
    os.makedirs(path_to_make)

exist\u ok
在3.2版中是“新的”否,这与wim的行为不同。@wim,真的吗?为什么不呢?我遗漏了什么?例如,如果名称为的文件存在(而不是目录)。从技术上讲,该检查不适用于路径的每个部分。此代码具有竞争条件(另一个进程可以在此进程的系统调用之间执行系统调用,以检查存在性和创建条目)。一种更健壮的方法是尝试创建eaoh目录,并捕获“目录已经存在”的错误。处理“名称存在但不是目录”是一个有趣的挑战。
Traceback (most recent call last):
  File "mk.py", line 3, in <module>
    os.makedirs('/root/test_{}'.format(a), exist_ok=True)
  TypeError: makedirs() got an unexpected keyword argument 'exist_ok'
if not os.path.exists(path_to_make):
    os.makedirs(path_to_make)
if os.path.exists(path_to_make):
    if not os.path.isdir(path_to_make):
        raise OSError("Cannot create a file when that file already exists: "
                      "'{}'".format(path_to_make))
else:
    os.makedirs(path_to_make)