【问题标题】:How to fix FileNotFoundError: [WinError 2] The system cannot find the file specified如何修复 FileNotFoundError: [WinError 2] 系统找不到指定的文件
【发布时间】:2019-04-02 07:13:21
【问题描述】:

我正在尝试重命名文件夹中的多个文件,以便删除每个字母,但运行时出现此错误:

FileNotFoundError: [WinError 2] 系统找不到文件 指定:'Amsterdam1971' -> '1971'

import os
os.chdir(directory)
for f in os.listdir():
    f_name, f_ext = os.path.splitext(f)
    os.rename(f_name,f_name.translate(str.maketrans("","","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")))

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    您正面临此问题,因为您尝试更新不带扩展名的文件名。您应该添加文件扩展名来解决此问题。

    import os
    os.chdir(directory)
    for f in os.listdir():
        f_name, f_ext = os.path.splitext(f)
        os.rename(f_name+'.'+f_ext,f_name.translate(str.maketrans("","","ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"))+'.'+f_ext)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 1970-01-01
      相关资源
      最近更新 更多