在 Os.Rename 中强制覆盖 [英] Force Overwrite in Os.Rename

查看:189
本文介绍了在 Os.Rename 中强制覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果另一个文件已经存在,是否可以强制重命名 os.rename 以覆盖另一个文件?例如,在下面的代码中,如果文件 Tests.csv 已经存在,它将被 Tests.txt 文件(也被重命名为 Tests.csv)替换.

Is it possible to force a rename os.rename to overwrite another file if it already exists? For example in the code below if the file Tests.csv already exists it would be replaced by the Tests.txt file (that was also renamed to Tests.csv).

os.rename("C:\Users\Test.txt","C:\Users\Tests.csv");

推荐答案

你可以试试 shutil.move():

You could try shutil.move():

from shutil import move

move('C:\\Users\\Test.txt', 'C:\\Users\\Tests.csv')

或者 os.remove 然后 shutil.move:

from os import remove
from shutil import move

remove('C:\\Users\\Tests.csv')
move('C:\\Users\\Test.txt', 'C:\\Users\\Tests.csv')

这篇关于在 Os.Rename 中强制覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆