[英]How to resolve FileExistsError: [WinError 183] Cannot create a file when that file already exists?
This error has been asked multiple times in this forum and I tried possible solution but I could not resolve the issue.此错误已在此论坛中被多次询问,我尝试了可能的解决方案,但无法解决问题。 It does rename the file successfully but still annoying because it gives the same error message.
它确实成功地重命名了文件,但仍然很烦人,因为它给出了相同的错误消息。 Appreciate your input/help on this.
感谢您对此的输入/帮助。 Thank you very much.
非常感谢你。
Error:错误:
os.rename("Master.xlsx", "Old_Master.xlsx")
FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'Master.xlsx' -> 'Old_Master.xlsx'
Code:代码:
os.chdir("C:/Path")
for file in os.listdir():
if file.endswith(".xlsx"):
if os.path.exists("Master.xlsx"):
os.rename("Master.xlsx", "Old_Master.xlsx")
os.rename(file, "Master.xlsx")
I got this code from someone here, the answer was deleted.我从这里的某人那里得到了这段代码,答案被删除了。 Thanks
谢谢
You are trying to rename all excel file to the same name, that can not be done.您正在尝试将所有 excel 文件重命名为相同的名称,这是无法完成的。 instead try to rename files with some change in names like this.
而是尝试使用这样的名称更改来重命名文件。
for count, file in enumerate(os.listdir()):
if file.endswith(".xlsx"):
os.rename(file, f"Master{count}.xlsx")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.