2013-07-25 118 views
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,如果这能帮助任何。

我该怎么办才能绕过此错误并允许我更改文件名?

+1

这是Python 3.X,正确吗? ('os.listdir()'在2.X上抛出一个异常,除非你传递一个路径) – FakeRainBrigand

+0

尝试将原始文件名转换为Unicode。您的循环会将多字节字符分解为单个字节,并且其中一些可能是无效的文件名字符,即使它们可打印。 –

+0

@MarkRansom:'file_name'应该是Unicode字符串(可选路径等于''。'(Unicode字符串),因此'listdir()'必须返回Unicode字符串)。 – jfs

回答

6

在这里你去,这对Python 2.7以及

import os 
import string 

for file_name in os.listdir(src_dir): 
    new_file_name = ''.join(c for c in file_name if c in string.printable) 
    os.rename(os.path.join(src_dir,file_name), os.path.join(src_dir, new_file_name)) 

干杯!如果您发现此答案有用,请不要忘记进行投票。 ;)

+0

谢谢,我还没有用正确的utf-8和正则表达式来做到这一点,但至少它工作正常。 – Omiod