I have the file name, "abc枚.xlsx", containing some kind of non-ASCII character encoding and I'd like to remove all non-ASCII characters to rename it to "abc.xlsx".
Here is what I've tried:
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)
The following error results at os.rename()
:
builtins.WindowsError: (2, 'The system cannot find the file specified')
This is on a Windows system, sys.getfilesystemencoding()
gives me mbcs
, if that helps any.
What should I do to circumvent this error and allow me to change the file name?