【发布时间】:2021-03-21 21:26:07
【问题描述】:
我是 python 语言的新手,正在尝试编写一个脚本,可以将文件名的最后一个字符切成特定的长度。 它有效,但由于某种原因,它拒绝继续,并且循环意外中断,给出文件不存在的错误消息。 错误消息:“FileNotFoundError:[WinError 2] 系统找不到指定的文件:” 这是我的脚本,请告诉我有什么问题!!!
import os
#define a function to trim latest characters to a specefic length"""
def renamer(folderpath, newlength):
while True:
for filename in os.listdir(folderpath):
root = os.path.splitext(filename)[0]
exten = os.path.splitext(filename)[1]
while len(root) >= newlength:
os.rename(folderpath + '\\' + root + exten, folderpath + '\\' + root[:-1] + exten)
continue
if len(root) <= newlength:
break
【问题讨论】:
-
您收到了什么错误信息?也可以使用
os.path.join()来连接路径,而不是使用字符串添加。 -
错误信息:FileNotFoundError: [WinError 2] 系统找不到指定的文件:
-
请编辑问题并包含相关错误信息
标签: python-3.x