Python-WindowsError:[错误2]系统找不到指定的文件 [英] Python - WindowsError: [Error 2] The system cannot find the file specified

查看:260
本文介绍了Python-WindowsError:[错误2]系统找不到指定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个装满pdf文件的文件夹.我正在尝试从文件名中删除所有空格,并用下划线替换它们.这是我到目前为止的内容:

I have a folder full of pdf files. I'm trying to remove all the spaces from files name and replace them with underscores. Here's what I have so far:

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)

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

When I run this script I get the following error:

WindowsError: [Error 2] The system cannot find the file specified

我猜有一个非常简单的修复程序,但是我四处张望,找不到适合我一生的解决方案.

I'm guessing there is a pretty simple fix, but I've look all over and cannot find a solution for the life of me.

感谢您的帮助!

推荐答案

...

os.rename(os.path.join(folder, files), os.path.join(folder, NewName))

这篇关于Python-WindowsError:[错误2]系统找不到指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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