I have 2 folders, let’s call them dir1 and dir2.
Is there a simple way to move all files from dir1 to dir2 and vice-versa?
>Solution :
I would use a third temporary folder:
from pathlib import Path
import shutil
def move_files(src_path, trg_path):
for src_file in Path(src_path).glob('*.*'):
shutil.copy(src_file, trg_path)
move_files('/path/to/dir1/', '/path/to/tmp/')
move_files('/path/to/dir2/', '/path/to/dir1/')
move_files('/path/to/tmp/', '/path/to/dir2/')
You need a temporary folder because there could be files that have the same name. The temporary folder must be empty.