【发布时间】:2023-03-07 22:49:01
【问题描述】:
我正在使用以下函数从文件名中删除数字。
import os
def rename_files():
file_list = os.listdir(r"C:\Users\X\Downloads\Compressed\prank\prank")
## print(file_list)
os.chdir(r"C:\Users\X\Downloads\Compressed\prank\prank")
print(os.getcwd())
for file_name in file_list:
os.rename(file_name, file_name.translate(None, "1234567890"))
rename_files()
它在 python 2 中完美运行,但在 python 3 中它会引发以下错误。
Traceback (most recent call last):
File "C:\Python34\rename_files.py", line 11, in <module>
rename_files()
File "C:\Python34\rename_files.py", line 9, in rename_files
os.rename(file_name, file_name.translate(None, "1234567890"))
TypeError: translate() takes exactly one argument (2 given)
如何在不使用正则表达式的情况下解决这个问题。
【问题讨论】:
标签: python python-2.7 python-3.x