m0_75004425 2022-11-08 10:56 采纳率: 50%
浏览 26
已结题

os.rename(os.path.join( , ),os.path.join( , )+'.pdf')批量修改文件名。出现了参数错误,如何解决?

os.rename(os.path.join( , ),os.path.join( , )+'.pdf')批量修改文件名。出现了参数错误

问题详细如下:
X[WinError 87] 参数错误。: 'C:\Users\shinelon\.spyder-py3\111111\ERL-00-9.pdf' -> 'C:\Users\shinelon\.spyder-py3\111111\9Global Data Plane: A Widely Distributed Storage and Communication Infrastructure.pdf'

#我的目的是想用txt文本里面写好的东西改文件名
#这是我的代码

import os
#含我们要用的文件名的列表
with open(r"11.txt","r",encoding="utf-8") as f:
    liness=[]
    for i in f:
        b = i.strip()
        liness.append(b)
    #liness.rstrip()
    print(liness)
paths = r"C:\Users\shinelon\.spyder-py3\111111"     

# 待修改文件夹内文件名列表
fileList = os.listdir(paths) 
#将列表名按末尾的数字排列
def list_sort(x : str):
    if '.' in x:
        # 将文件名字用_进行分割
        x = x.rpartition('-')[2]
        # 将x用.进行分割,最后拿到数字
        x = int(x.split('.')[0])
    else:
        x = 0
    return int(x)
    # 排序前
print(fileList)
# 进行排序
fileList.sort(key=list_sort)        
# print(type(fileList))
# 输出文件夹中包含的文件

# print("修改前:" + str(fileList))
b=0
for a in fileList:
    os.rename(os.path.join(paths,a),os.path.join(paths,liness[b])+'.pdf')
    b+=1

#以下是执行结果
['10Metal: A Metadata-Hiding File Sharing System', '11Novel Computing Paradigms using Oscillators', '12Deep-learning-based Machine Understanding of Sketches: Recognizing and Generating Sketches with Deep Neural Network', '13A Fully Integrated Electronic-Photonic Platform for Label\x02Free Biosensing', '14Explainable and Advisable Learning for Self-driving Vehicles', '15Design of a sub-Hz Resolution Fully Digital RF Frequency Synthesizer', '16Low Noise Integrated CMOS Receiver Front-End', '17Reinforcement Learning for Robotic Assembly with Force Control', '18Analysis of Factors and Interventions Relating to Student Performance in CS1 and CS2', '19Building XP process metrics for project-based software engineering courses', '20Geometric Sampling Theory, Triangulations, and Robust Machine Learning']

['EECS-2020-11.pdf', 'EECS-2020-12.pdf', 'EECS-2020-13.pdf', 'EECS-2020-15.pdf', 'EECS-2020-16.pdf', 'EECS-2020-17.pdf']
Traceback (most recent call last):

  File "C:\Users\shinelon\AppData\Local\Temp\ipykernel_6036\1984612430.py", line 36, in <module>
    os.rename(os.path.join(paths,a),os.path.join(paths,liness[b])+'.pdf')

OSError: [WinError 87] 参数错误。: 'C:\\Users\\shinelon\\.spyder-py3\\11111\\EECS-2020-11.pdf' -> 'C:\\Users\\shinelon\\.spyder-py3\\11111\\10Metal: A Metadata-Hiding File Sharing System.pdf'


```python



```

  • 写回答

4条回答 默认 最新

  • honestman_ 2022-11-08 10:59
    关注

    应该是路径里面包含点导致的,试试把点去了:

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 11月16日
  • 已采纳回答 11月8日
  • 创建了问题 11月8日

悬赏问题

  • ¥15 双向孟德尔随机化反向是为什么显示参数长度为零
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常
  • ¥15 github上的这个C语言项目如何跑起来
  • ¥15 java 判断某个数 区间是否存在
  • ¥15 appium控制多个雷电模拟器问题
  • ¥15 C# iMobileDevice