Move and replace if same file name already existed in python
以下是将移动和替换单个文件的代码
1 2 3 4 5 6 7 8 9 10 11 | import shutil import os src = 'scrFolder' dst = './dstFolder/' filelist = [] files = os.listdir( src ) for filename in files: filelist.append(filename) fullpath = src + '/' + filename shutil.move(fullpath, dst) |
如果我执行相同的命令并移动已经存在于
如果指定目标的完整路径(而不仅仅是目录),那么
1 | shutil.move(os.path.join(src, filename), os.path.join(dst, filename)) |
我通过在移动命令中为源和目标提供完整路径来覆盖它...
记得为Windows路径添加双斜杠。
1 2 3 4 5 6 7 8 9 10 11 12 | # this is to change directories (type your own) os.chdir("C: EPORTS\DAILY_REPORTS") # current dir (to verify) cwd = os.getcwd() src = cwd dst = cwd + '\\XLS_BACKUP\' shutil.move(os.path.join(src, file), os.path.join(dst, file)) # nice and short. |