简体   繁体   English

如何解决 FileExistsError: [WinError 183] 当文件已经存在时无法创建文件?

[英]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.

相关问题 FileExistsError:[WinError 183] 当文件已存在时无法创建文件 - FileExistsError: [WinError 183] Cannot create a file when that file already exists 为什么我收到错误消息:“FileExistsError: [WinError 183] 当文件已存在时无法创建文件”? - Why am I getting the error: "FileExistsError: [WinError 183] Cannot create a file when that file already exists"? Winerror 183 文件已存在时无法创建文件 - Winerror 183 Cannot create a file when that file already exists 重命名txt文件。 编辑版本:[错误183]该文件已存在时无法创建该文件 - Rename txt file. Edited version: [Error 183] Cannot create a file when that file already exists Python无法创建文件错误[错误:183] - Python Cannot create a file error [Error : 183] Google App Engine:“文件已经存在时无法创建” - Google App Engine: “Cannot create a file when that file already exists” FileNotFoundError: [WinError 2] 使用ffmpeg-python时系统找不到指定的文件如何解决? - how to resolve error FileNotFoundError: [WinError 2] The system cannot find the file specified when using ffmpeg-python? Python 创建目录错误 当文件已存在时无法创建文件 - Python create directory error Cannot create a file when that file already exists 使用Python观察到该文件已存在时无法创建文件 - Cannot create a file when that file already exists error observed using Python python os.rename“”在该文件已存在时无法创建该文件 - python os.rename “”cannot create a file when that file already exists
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM