2017-02-20 10 views
0

これは最初の部分です。基本的には3つの新しい必須フォルダ(1,2,3)がメインのTESTディレクトリに作成されます。 。これは、今、私は、次の処理を行い、新たなスクリプトが必要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で作成されたすべてのフォルダをコピーコピーされたフォルダ。

アイデア? ありがとうございました!

答えて

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) 
+0

ありがとうタイタン、それは作業フォルダを作成しますが、何らかの理由で、作成したフォルダをPython_1から作業フォルダにコピーしません。 – user7380301

+0

私はlist_dir = []は空リストであり、下の行は決して実行されないと思います。これを修正するには? – user7380301

+0

@ user7380301、 'list_dir = []'の後のコメントに 'Folder1、Folder2、Folder3 from Python_1'と表示されます。つまり、 'list_dir'では' Python_1'からフォルダの名前を指定する必要があります。あなたの質問から、あなた自身がこれらのフォルダ名を提供したいのかどうか、あるいはこれらのフォルダを自動的に見つけて回答を更新できるコードを追加したいのかどうかはわかりません。 – TitanFighter

関連する問題