我想将文件移动并覆盖到另一个具有相同文件的文件夹:
d_folder = shutil.move(min_file, d_folder)
print("File is moved successfully to: ", d_folder)
Shuutil 移动不会覆盖文件。有没有其他方法可以移动和覆盖文件?
shutil.Error: Destination path 'C:\\g.txt' already exists
你不能在一行中完成,你必须这样做:
import os, shutil
if os.path.isfile(os.path.join(d_folder, min_file)):
os.remove(os.path.join(d_folder, min_file))
d_folder = shutil.move(min_file, d_folder)
print("File is moved successfully to: ", d_folder)
这会检查文件是否已经存在,如果存在则将其删除,然后将新文件移动到那里。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句