How to rename a file with non-ASCII character encoding to ASCII

ascii, filenames, python, python-3.x, unicode

Solution

Here you go, this works with python 2.7 as well

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))

Cheers! Don't forget to up-vote if you find this answer useful! ;)

Problem

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?

Original source