所以我有一个目录,用今天的日期创建,用户选择三个整数。 在这个目录中,我创建了许多子文件夹,名为ARI,Ale,Cdl,MXF,Reports等等。
我想创建更多子文件夹,但只为一个子文件夹创建'ARI'文件夹。
如何指定我想要创建名为“Main Unit”和“Second Unit”的文件夹?
以下是我到目前为止所做的事情 - 感谢任何帮助:)
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
from __future__ import print_function
import os
import datetime
try: #Handle Python 2.7
input = raw_input
except NameError:
pass
deepfolders = ("Main Unit", "Second Unit")
subfolders = ("ari", "sound", "cdl" , "mxf" , "h264" , "reports" , "ale")
parent_format = "{0:03d}_{1:%y%m%d}_Unit_Project"
path = ('/Volumes/Macintosh HD/Users/thenightfactory/Documents')
path2 = ('/Volumes/Macintosh HD/Users/thenightfactory/Pictures')
os.chdir(path)
from datetime import datetime
weekday = datetime.strftime(datetime.now(), "%A")
print ("")
print ("Hello, Happy " + weekday + "! Please Select A Shooting Day To Create Today's Folders:")
print ('\033[91m' + "This will only work until midnight and can only be three integers eg. 023" + '\033[0m')
print("=" * 80)
while True:
try:
shootnum = (int(input("Enter today's shoot number > ")))
shootnum = (str(shootnum))
shootnum = int(shootnum[:3])
successful= True
if successful:
print ("")
print ('\033[91m' + "Folder's Have Been Created, Good Luck Shooting!" + '\033[0m')
print ("")
break
except ValueError:
print('\033[91m' + "Invalid number, please try again." + '\033[0m')
import datetime
parentpath = parent_format.format(shootnum, datetime.datetime.now())
if not os.path.exists(parentpath):
os.makedirs(parentpath)
for subfolder in subfolders:
os.makedirs(os.path.join(parentpath, subfolder))
os.chdir(path2)
parentpath = parent_format.format(shootnum, datetime.datetime.now())
if not os.path.exists(parentpath):
os.makedirs(parentpath)
for subfolder in subfolders:
os.makedirs(os.path.join(parentpath, subfolder))
答案 0 :(得分:2)
假设你想要一个(半)通用解决方案,解决问题的一种方法是将所有位加在一起,因为join可以使用多个参数。因此,在'ari'
子文件夹中运行时添加一个额外的循环:
deepsubs = ['ari']
if not os.path.exists(parentpath):
os.makedirs(parentpath)
for subfolder in subfolders:
os.makedirs(os.path.join(parentpath, subfolder))
if subfolder in deepsubs:
for deepfolder in deepfolders:
os.makedirs(os.path.join(parentpath, subfolder, deepfolder))
答案 1 :(得分:1)
我的方法只是检查子文件夹值并下降到它并创建所需的目录。
parentpath = parent_format.format(shootnum, datetime.datetime.now())
if not os.path.exists(parentpath):
os.makedirs(parentpath)
for subfolder in subfolders:
pwd = os.getcwd()
os.makedirs(os.path.join(parentpath, subfolder))
# Checking in list in case you want todo same for multiple dir
# else just eqality check would do
if subfolder in ['ari']:
os.chdir(os.path.join(parentpath, subfolder))
for df in deepfolders:
os.makedirs(os.path.join(parentpath, subfolder, df))
os.chdir(pwd)
如果这不是你想要的,请告诉我。