【问题标题】:How can I create multiple directories using a loop in python?如何在 python 中使用循环创建多个目录?
【发布时间】:2017-04-20 19:31:37
【问题描述】:

我想用循环创建 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 相当陌生,这段代码对我来说很有意义,我不知道为什么它可能不会工作。任何帮助表示赞赏。

【问题讨论】:

  • 你需要在循环中更新directory
  • @MooingRawr 哇,这太简单了。非常感谢!

标签: python loops


【解决方案1】:
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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 2019-01-22
    • 2017-07-19
    • 2019-03-07
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    相关资源
    最近更新 更多