import os import sys #--> Just for sys.exit(). Could also use exit() instead. import shutil import logging TopDIR = "./Move-Files-Safely_test-folder/" Sources = ["Source the first","Source the second"] #--> Directories to move folders to/from SourceIdx = 0 #--> Index of the source to copy from DestinationIdx = 1 - SourceIdx #--> flipps between 0 and 1 # Set the source and destination directories: SourceDIR = TopDIR + Sources[SourceIdx] if SourceDIR[-1] != "/": SourceDIR += "/" DestinationDIR = TopDIR + Sources[DestinationIdx] if DestinationDIR[-1] != "/": DestinationDIR += "/" # Get a list of the folders: SourceDirectories = [] DestinationDirectories = [] file_list = os.listdir(SourceDIR) for file in file_list: FullPath = SourceDIR+file DestPath = DestinationDIR+file if os.path.isdir(FullPath): #--> Only include folders if file[0] != ".": #--> Don't allow system files (folders that begin with ".") if FullPath[-1] != "/": FullPath += "/" if DestPath[-1] != "/": DestPath += "/" SourceDirectories.append(FullPath) DestinationDirectories.append(DestPath) if SourceDirectories == []: print("\nNo folders in '{}'!".format(Sources[SourceIdx])) print("Stopping program!\n") sys.exit() ###print(SourceDirectories) ###print("") ###print(DestinationDirectories) # Move the folders: for idx,folder in enumerate(SourceDirectories): try: shutil.copytree(folder, DestinationDirectories[idx]) #--> First copy the file shutil.rmtree(folder) #--> If that succedes delete the original print("{} moved!".format(folder)) except Exception as error: logging.exception('Caught an error') sys.exit() #--> Can also simply use exit(), but the sys method is often preferred. print("\nJob Done!")