Python - WindowsError: [错误 2] 系统找不到指定的文件

Mat*_*att 1 python filenames

我有一个装满pdf文件的文件夹。我试图从文件名中删除所有空格并用下划线替换它们。这是我到目前为止所拥有的:

import os, sys

folder = path to folder
FileList = os.listdir(folder)

for files in FileList:
    if ' ' in files:
        NewName = files.replace(" ", "_")
        os.rename(files, NewName)
Run Code Online (Sandbox Code Playgroud)

当我运行此脚本时,出现以下错误:

WindowsError: [Error 2] The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)

我猜有一个非常简单的解决方法,但我已经找遍了所有地方,找不到适合我生活的解决方案。

谢谢您的帮助!

Ign*_*ams 5

...

os.rename(os.path.join(folder, files), os.path.join(folder, NewName))
Run Code Online (Sandbox Code Playgroud)