当具有范围条件时,如何使用for循环创建新的子文件夹?

2024-04-28 16:06:07 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我每天都在尝试用python自动创建新的目录文件夹。我需要根据我的列表创建新的修复子文件夹。像这样:

enter image description here

这是我的剧本:

import os
from datetime import date
today = date.today()
d1 = today.strftime("%d-%m-%Y")
#Ie Source Path
ie = ["G:/01-FIN & ACC-Confidential/Inbound/IE-Direct/",
"G:/02-BDSM (ห้ามย้ายเด็ดขาด)-Confidential/Inbound/IE-Direct/",
"G:/03-BSC-Confidential/Inbound/IE-Direct/",
"G:/04-CLD-Confidential/Inbound/IE-Direct/",
"G:/05-COMPLIANCE-Confidential/Inbound/IE-Direct/",
"G:/06-CRD-Confidential/Inbound/IE-Direct/",

# define the access rights
access_rights = 0o755

team = ["ACCOUNTING","BDSM","BSC","CLD","COMPLIANCE","CRD","HR","IT","MGT","MKT","OPS","COPORATE SALES","TELESALES","PASSAPORN","FRAUD","LEGAL","INS","BIQM",]

for i,j in ie+team:
    os.mkdir(i+str(d1)+"/"+j)

请告诉我如何在已使用当前程序创建的文件夹中创建子目录


Tags: import文件夹todaydateosied1direct
1条回答
网友
1楼 · 发布于 2024-04-28 16:06:07

我试过你的代码,但它说太多的值解包,然后我修改你的代码创建6个目录,然后再次创建3个子目录在这6个目录

from datetime import date
today = date.today()
d1 = today.strftime("%d-%m-%Y")
#Ie Source Path
ie = ["D:/t/",
"D:/t/",
"D:/t/",
"D:/t/",
"D:/t/",
"D:/t/"]

# define the access rights
access_rights = "0o755"

team = ["ACCOUNTING","BM","BSC","CLD","COMPLIANCE","CRD"]//actual 6 directories 
new_subs=["1","2","3"] //names of 3 subdirectories

os.mkdir(ie[0]+str(d1))//main directory named as today's date

for i in range(0,6):
    os.mkdir(ie[i]+str(d1)+"/"+team[i])
    for j in range(len(new_subs)):
        os.mkdir(ie[i]+str(d1)+"/"+team[i]+"/"+new_subs[j])

如果他们对代码有任何疑问,请发表评论

相关问题 更多 >