在目录中创建多个子文件夹

时间:2018-11-05 22:53:49

标签: python python-3.x

我正在尝试创建一个程序,以在创建的每个文件夹中创建多个文件夹和2个文件夹。按照下面的程序,我只能创建1个子文件夹。

import os
root_path="C:\\Users\\Master\\Desktop\\SERIAL"
for i in range(20180812, 20180818):
    os.makedirs(os.path.join(root_path, str(i), 'name'))

1 个答案:

答案 0 :(得分:1)

您可以在循环中添加更多文件夹:

import os

root_path = "./"
subsfolders_to_create = ['name','age','size']

for i in range(20180812, 20180818):
    for subfolder in subsfolders_to_create :
        os.makedirs(os.path.join(root_path, str(i), subfolder))

for root,dirs,files in os.walk("./"):
    for d in dirs:
        print (os.path.join(root,d))

输出:

./20180813
./20180814
./20180815
./20180812
./20180816
./20180817
./20180813/size
./20180813/age
./20180813/name
./20180814/size
./20180814/age
./20180814/name
./20180815/size
./20180815/age
./20180815/name
./20180812/size
./20180812/age
./20180812/name
./20180816/size
./20180816/age
./20180816/name
./20180817/size
./20180817/age
./20180817/name