Python用增量形式创建目录循环

2024-04-16 23:36:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我想脚本创建一个新的目录与相同的名称已经到位的基础上给定的目录。这样,如果文件夹0存在,但文件夹1不存在,它将创建文件夹1并在一次目录创建时停止。你知道吗

import os

i=1
while True:
    path = "Folder_{}/".format(i)
    os.makedirs(os.path.dirname("Folder_{}/".format(i)), exist_ok=False)
    if not os.path.exists(path):
        os.makedirs(os.path.dirname("Folder_{}/".format(i)), exist_ok=False)
    i += 1

我已经测试了上面的代码,它让我非常头痛,因为它创建了大约10K目录/秒哈哈。提前谢谢!你知道吗


Tags: pathimport目录脚本文件夹名称falseformat