【问题标题】:create multiple folders using loops in python [closed]在python中使用循环创建多个文件夹[关闭]
【发布时间】:2017-06-19 10:57:11
【问题描述】:

我想在 python 中编写一个函数,每次给定条件为真时创建一个文件夹。我不知道这个条件会满足多少次。会像

step_1 条件为真创建文件夹1 step_2 条件为真 创建文件夹2 ... step_n 条件为 true 创建文件夹n

【问题讨论】:

  • 您可以使用 os.makedirs 创建新文件夹(并使用 os.path.exists 来检查文件夹是否存在) - 如果这就是您的要求。
  • 不,我要问的是如何在每次条件为真时自动递增计数器,而不使用 for 循环,因为我不知道条件会满足多少次。
  • 当你的条件为真时,继续循环
  • 那么你想要while evaluate_condition():,问题应该是'当条件是True而不是创建文件夹时,我该如何循环。

标签: python for-loop while-loop mkdir


【解决方案1】:

带有os.mkdiros.mkdirs 的循环

https://docs.python.org/2/library/os.html

os.mkdir(path[, mode])

使用数字模式创建一个名为 path 的目录。默认模式为 0777(八进制)。如果目录已经存在,OSError 是 提出来。

在某些系统上,模式被忽略。在使用它的地方,当前的 umask 值首先被屏蔽掉。如果最后 9 位以外的位(即 模式的八进制表示的最后 3 位)被设置, 它们的含义取决于平台。在某些平台上,它们是 忽略,您应该显式调用 chmod() 来设置它们。

也可以创建临时目录;查看 tempfile 模块的 tempfile.mkdtemp() 函数。

可用性:Unix、Windows。

os.makedirs(path[, mode])

递归目录创建函数。与 mkdir() 类似,但使所有需要包含叶目录的中间级目录。 如果叶目录已存在或引发错误异常 无法创建。默认模式为 0777(八进制)。

mode参数被传递给mkdir();请参阅 mkdir() 说明了解它的解释方式。

类似:

import os

for i in range(n):
    # makeadir() evaluates your condition
    if makeadir(i):
        path = 'folder {}'.format(i)
        if not os.path.exists(path):
            os.mkdir(path)

编辑:如果你有一个条件:

import os

i = 1
while eval_condition():
    path = 'folder {}'.format(i)
    if not os.path.exists(path):
        os.mkdir(path)
    i += 1

【讨论】:

  • 问题是我不知道n(条件为真多少次)
  • 那么了解您的标准是什么会有所帮助。如果可以将其编码为生成器函数,则可以在 for 循环中使用它,并完全跳过 if makeadir()
  • @ufdul 编辑怎么样?
  • 我正在尝试检查图像标签是否离图像边缘不太近。如果此条件为真,那么我将创建一个文件夹并将此图像保存到该文件夹​​中。
  • 那又怎样?你有图片列表吗?以我的第一个示例为例,将 for 循环更改为 for i, image in enumerate(imagelist): 并以 makeadir(image) 为标准。这对你有用吗?您真的应该将此信息添加到问题中。这个问题让我们在这里猜测很多。
【解决方案2】:
import os

condition_success = 0 # set initial 0 
while True:
    condition_success += 1 # get counter for condition to increment if condition is true:
    # By default this will create folder within same directory
    os.makedirs("folder"+str(condition_success)) # creates folder1 if condition_success is 1

在其他地方创建目录设置路径

path = "/path/"
os.makedirs(path + "folder"+str(condition_success))

或者你可以直接创建为

  os.makedirs("/path/folder"+str(condition_success))

替代方式:

如果你想在子条件中使用那个条件,你可以使用 if 语句来执行它或打破你的条件以防止无限 循环

condition_success = 0
usr_input = int(input("Enter number to create number of folder/execute condition: ")) # get number from user input
while True:
  condition_success += 1 # get counter for condition to increment if condition is true:
  # By default this will create folder within same directory
  os.makedirs("folder"+str(condition_success)) # creates folder1 if condition_success is 1
  if condition_success >= usr_input:
      break

【讨论】:

    【解决方案3】:

    最简单的方法是使用 while 循环并使用计数器来计算循环的次数。

    import os
    
    counter=1
    while statement:
        os.mkdir('folder{}'.format(str(counter)))
        counter += 1
        # give a new value to your statement to keep creating or stop creating directories
        statement = true 
    

    【讨论】:

      猜你喜欢
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 2013-06-29
      • 2014-01-01
      • 2013-01-08
      • 1970-01-01
      • 2013-01-06
      • 2022-06-16
      相关资源
      最近更新 更多