Skip to main content
 首页 » 编程设计

python之如何在 python 中重命名文件名

2025年05月04日27pengyingh

<分区>

我有一个文件列表。每个文件名都包含一个我想删除的数字。然后我想用新文件名(没有数字)重命名文件。出现以下错误:

代码如下:

import os, re 
mypath = "/Users/tikka/Downloads/prank/prank" 
for f in os.listdir(mypath): 
 print f 
 frenamed = re.sub('\d','',f) 
 print frenamed 
 os.rename(f,frenamed) 
 print f 

错误信息

Traceback (most recent call last): 
  File "C:/Python27/ty3.py", line 7, in <module> 
    os.rename(f,frenamed) 
WindowsError: [Error 2] The system cannot find the file specified 

这个问题不同于Rename Files in Python因为我的 .py 与我希望重命名的文件不在同一目录中