这是第一个有效的部分,基本上在主TEST目录中创建了3个新的强制文件夹(1,2,3),如果用户想要添加任何新的强制文件夹,他们可以这样做。这是Python_1
import os
root_path = r"C:\TEST"
list_dir = []
while True:
userinput1 = raw_input("Enter the name for Folder1, Folder2, Folder3:")
list_dir.append(userinput1)
userinput2 = None
#ask user to respond 'yes' or 'no' as to whether they want to add another directory
while userinput2 != "yes" and userinput2 != "no":
userinput2 = raw_input("Would you like to add another directory? yes/no: ")
if userinput2 == "no":
break
for directory in list_dir:
os.mkdir(os.path.join(root_path, directory))
print 'New directories have been created'
现在我需要一个执行以下操作的新脚本: 在TEST中创建新的主文件夹WORKING,并将在Python_1中创建的所有文件夹复制到新创建的WORKING文件夹中,并附加_working到复制文件夹的名称。
有什么想法吗? 非常感谢你的帮助!
答案 0 :(得分:0)
import os
import shutil
root_path = r"C:\TEST"
new_main_folder = 'WORKING'
list_dir = next(os.walk(root_path))[1] # Folder1, Folder2, Folder3 from Python_1
# Creates C:\TEST\WORKING
new_root_path = os.path.join(root_path, new_main_folder)
os.mkdir(new_root_path)
for directory in list_dir:
src = os.path.join(root_path, directory) # C:\TEST\Folder[1,2,3]
dest = os.path.join(new_root_path, directory + '_working') # C:\TEST\WORKING\Folder[1,2,3]_working
# or
# renamed_folder = directory + '_' + new_main_folder.lower()
# dest = os.path.join(new_root_path, renamed_folder)
shutil.copytree(src, dest)