简体   繁体   English

在Python中创建多个子目录

[英]Make Multiple Subdirectories In Python

I am doing a kind of special code right here. 我在这里正在做一种特殊的代码。 I am making a code that will create 1 - 10 directories called box1, box2... until 10. In these directories there will be other directories. 我正在编写将创建1-10个名为box1,box2 ...的目录,直到10的代码。在这些目录中将有其他目录。 And in these directories more subdirectories... . 在这些目录中还有更多子目录...。 At the end, it will generate a key. 最后,它将生成一个密钥。 Here is what I have for now: 这是我现在拥有的:

import time
import random
import subprocess
import os
import os.path

liste = []
old = []
key = False
numberOfBoxes = 5
files = -1
number = random.randint(1, 10)

def random_generator():
    for looper in range(1, numberOfBoxes):
        rand = random.randint(1, 10)
        liste.append(rand)


def calculate():
    c = liste[0]
    for looper in range(1, c):
        print(looper)
        newpath = 'Box' + str(looper)
        if not os.path.isdir('./' +newpath+'/'):
            os.mkdir('./' +newpath+'/')


def subdir_true():
    files = -1
    read = liste[0]
    chance = random.randint(1, 2)
    for looper in range(1, read):
        if chance == 1:
            for looper in range(1, read):
                exist = os.path.exists('Box' + str(looper))
                if exist == True: 
                    newpath = 'Box' + str(looper)
                    old.append(newpath)
                    files += 1
                    oldF = old[files]
                    number = random.randint(1, 10)
                    for looper in range(1, number):
                        newpath = 'Box' + str(looper)
                        os.mkdir('./'+oldF+'/'+newpath+'/')


random_generator()
calculate()
subdir_true()

How do I make subdirectories into subdirectories using for looper in range() ? 如何for looper in range()使用for looper in range()将子目录变成子目录?

See the docs for os.makedirs() 请参阅os.makedirs()的文档

Recursive directory creation function. 递归目录创建功能。 Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory. 与mkdir()相似,但是使所有中间级目录都包含叶子目录。 Raises an error exception if the leaf directory already exists or cannot be created. 如果叶目录已经存在或无法创建,则引发错误异常。 The default mode is 0777 (octal). 默认模式是0777(八进制)。 On some systems, mode is ignored. 在某些系统上,模式被忽略。 Where it is used, the current umask value is first masked out. 使用它时,当前的umask值将首先被屏蔽掉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM