4
我有包含某种非ASCII字符编码的文件名“abc枚.xlsx”,并且想要删除所有非ASCII字符将其重命名为“abc.xlsx”。如何将非ASCII字符编码的文件重命名为ASCII
这里是我试过:
import os
import string
os.chdir(src_dir) #src_dir is a path to my directory that contains the odd file
for file_name in os.listdir():
new_file_name = ''.join(c for c in file_name if c in string.printable)
os.rename(file_name, new_file_name)
以下错误结果在os.rename()
:
builtins.WindowsError: (2, 'The system cannot find the file specified')
这是在Windows系统上,sys.getfilesystemencoding()
给我mbcs
,如果这能帮助任何。
我该怎么办才能绕过此错误并允许我更改文件名?
这是Python 3.X,正确吗? ('os.listdir()'在2.X上抛出一个异常,除非你传递一个路径) – FakeRainBrigand
尝试将原始文件名转换为Unicode。您的循环会将多字节字符分解为单个字节,并且其中一些可能是无效的文件名字符,即使它们可打印。 –
@MarkRansom:'file_name'应该是Unicode字符串(可选路径等于''。'(Unicode字符串),因此'listdir()'必须返回Unicode字符串)。 – jfs