0

I have list of files like below :

['/home/Test//A/Aa/hello1.c', '/home/Test/C/Aa/hello1.c', '/home/Test/B/Aa/hello1.c']

I'm trying to move all those files from that path to another path. When I find the same filename, I need to replace the existing one as I somehow wanted to remove those files from the existing path, so replace I need to do if same filename exists as they are same files.

Tried as below :

import shutil
list_l1 = ['/home/Test//A/Aa/hello1.c', '/home/Test/C/Aa/hello1.c', '/home/Test/B/Aa/hello1.c']
for source in list_l1:
    shutil.move(source, '/home/AShekar/sample_try/sample/')

I have received error as File "/usr/lib/python2.7/shutil.py", line 292, in move raise Error, "Destination path '%s' already exists" % real_dst

Thanks in advance!!

AMBATI
  • 1
  • 4
  • Check this https://stackoverflow.com/questions/31813504/move-and-replace-if-same-file-name-already-exists – Manik Tharaka Apr 30 '20 at 10:59
  • Does this answer your question? [Move and replace if same file name already exists?](https://stackoverflow.com/questions/31813504/move-and-replace-if-same-file-name-already-exists) – Anwarvic Apr 30 '20 at 10:59
  • No Sorry that scenario is different. Here I have file names too along with the path in the list. Moreover here for each file, the source path is different and also needs to traverse the list – AMBATI Apr 30 '20 at 11:03

1 Answers1

0

The snippet below will work on Python 3.8

    from shutil import copytree, rmtree
    import os
    src = os.path.join(os.getcwd(), 'src')
    dst = os.path.join(os.getcwd(), 'dst')
    copytree(src, dst, dirs_exist_ok=True)
    rmtree(src, ignore_errors=True)
Balaji Ambresh
  • 4,977
  • 2
  • 5
  • 17