Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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中使用循环创建多个目录?_Python_Loops - Fatal编程技术网

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

如何在python中使用循环创建多个目录?,python,loops,Python,Loops,我想创建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 导入操作系统 路径名=1 directory=“C:\dire

我想创建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  
导入操作系统
路径名=1
directory=“C:\directory\Path\Name\\”+str(路径名)
当路径名<11时:
如果不存在os.path.exists(目录):
os.makedirs(目录)
路径名+=1
但它只是创建了第一个目录并停止了,就好像它没有完成循环的其余部分一样。我对python相当陌生,这段代码对我来说很有意义,我不知道为什么它可能不起作用。非常感谢您的帮助

导入操作系统
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)
路径名=1 directory=“C:\directory\Path\Name\\”+str(路径名) 当路径名<11时: 如果不存在os.path.exists(目录): os.makedirs(目录) 路径名+=1 directory=“C:\directory\Path\Name\\”+str(路径名)
您需要更新loop@MooingRawr哇,太简单了。非常感谢!