我有如下的文件列表。
['homeTest/AAahello1.c', 'homeTestCAahello1.c', 'homeTestBAahello1.c'] 。
我试图将所有这些文件从该路径移动到另一个路径。当我发现相同的文件名时,我需要替换现有的文件名,因为我想从现有的路径中删除这些文件,所以如果存在相同的文件名,我需要替换,因为它们是相同的文件。
尝试如下。
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/')
我收到了错误信息,文件 "usrlibpython2.7shutil.py",第292行,在移动过程中出现错误,"Destination path '%s' already exists" % real_dst。
先谢谢你了!
下面的代码段将在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)