Python Forum
Copy folders to newly created folder and append - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Copy folders to newly created folder and append (/thread-2124.html)



Copy folders to newly created folder and append - Filthy_McNasty - Feb-20-2017

This is the first part that works , basically created 3 new mandatory folders (1,2,3) in the main TEST directory and if user wants to add any new one they can do so. This is 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'
Now I need a new script that does the following: Creates new main folder WORKING within TEST, and copy all folders created in Python_1 into newly created WORKING folder with appended _working to the name of the copied folders.

Any ideas?
Thank you very much for your help!


RE: Copy folders to newly created folder and append - Larz60+ - Feb-20-2017

change directory os.chdir
New folder os.mkdir (or use shutil)


RE: Copy folders to newly created folder and append - Filthy_McNasty - Feb-20-2017

But I have to use new script, I have to import Pytohn_1 into new script, not change directory and then I copy all created files ......


RE: Copy folders to newly created folder and append - wavic - Feb-20-2017

You can use shutil

import shutil

shutil.copytree(source, destination)
To rename folders you can use os.walk

for root, dirs, files in os.walk(path):
    if dirs:
        for directory in dirs:
            os.rename(directory, "{}_working".format(directory))



RE: Copy folders to newly created folder and append - Filthy_McNasty - Feb-21-2017

This created new Working folder but for some reason it does not coy created folders from Python_1, any idea where the error is:



import os
import shutil

root_path = r"C:\GEOS455\Assign2"
new_main_folder = 'Working'

new_root_path = os.path.join(root_path, new_main_folder)
os.mkdir(new_root_path)

list_dir = []  
for directory in list_dir:
    src = os.path.join(root_path, directory)  
    dest = os.path.join(new_root_path, directory + '_working')  
    shutil.copytree(src, dest)



RE: Copy folders to newly created folder and append - wavic - Feb-21-2017

However, my renaming snippet doesn't work. But if get any directory and put it with absolute path in a list then renaming is just iterate over the list.