Python Forum
Copy folders to newly created folder and append
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy folders to newly created folder and append
#1
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!
Reply
#2
change directory os.chdir
New folder os.mkdir (or use shutil)
Reply
#3
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 ......
Reply
#4
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))
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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)
Reply
#6
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 257 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 536 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Why does newly-formed dict only consist of last row of each year? Mark17 6 791 Nov-17-2023, 05:28 PM
Last Post: Mark17
  Create new folders and copy files cocobolli 3 1,446 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 1,002 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  Code to check folder and sub folders for new file and alert fioranosnake 2 1,933 Jan-06-2022, 05:03 PM
Last Post: deanhystad
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,476 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Move file from one folder to another folder with timestamp added end of file shantanu97 0 2,472 Mar-22-2021, 10:59 AM
Last Post: shantanu97
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,857 Dec-06-2020, 04:14 PM
Last Post: Divya577
  Python Cut/Copy paste file from folder to another folder rdDrp 4 5,047 Aug-19-2020, 12:40 PM
Last Post: rdDrp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020