如何在python中使用循环创建多个目录?

2024-06-17 12:41:50 发布

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

我想用一个循环创建10个目录,我尝试了以下代码:

import os
pathname = 1
directory = "C:\Directory\Path\Name\\" + str(pathname)


while pathname < 11:
    if not os.path.exists(directory):
        os.makedirs(directory)
    pathname += 1  

但它只是创建了第一个目录,然后停止了,好像它根本就没有经历过其他的循环一样,我对python还相当陌生,这段代码对我来说很有意义,我不知道为什么它可能不这样做工作。任何感谢帮助。在


Tags: path代码nameimport目录ifosexists
1条回答
网友
1楼 · 发布于 2024-06-17 12:41:50
import os
pathname = 1
directory = "C:\Directory\Path\Name\\" + str(pathname)

while pathname < 11:
    if not os.path.exists(directory):
        os.makedirs(directory)
    pathname += 1  
    directory = "C:\Directory\Path\Name\\" + str(pathname)

相关问题 更多 >