Search code examples
pythondirectoryoperating-systemshutil

Duplicate folder into new folder one level up in the directionary


I have a folder (1.0) which I want to duplicate one the folder one level up the directory. For example: C:\Users\remco\Downloads\stikstof-master\1.0 I either want the folder (1.0) or all the files within the folder (20 files) to get copied to: C:\Users\remco\Downloads\stikstof-master# Please note that the 2.0 folder is still non existent

I managed to get the current file path using the following code:

from utils import os
import shutil
file_path = os.getcwd()
print (file_path)

I tried some things within the os and shutil packages however I was just unable to get it working.


Solution

  • import shutil
    
    source_path = r'C:\Users\remco\Downloads\stikstof-master\1.0'
    destination_path = r'C:\Users\remco\Downloads\stikstof-master'
    
    try:
        shutil.copytree(source_path, destination_path, dirs_exist_ok=True)
    except Exception as e:
        print(f"Error occurred while copying directory: {e}")