Search code examples
python-3.xunicode

UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-7: ordinal not in range(128)


I would like to rename all the files in a certain directory. The old filename with a relative path is 'full_fname', after detoxing the filenames is 'full_new_fname' as in the picture. I am working in a Linux environment with Python 3.6 and using Jupyter notebook.

enter image description here

I use the following command to rename;

os.rename(full_fname,full_new_fname)

I get the error:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 2-7: ordinal not in range(128)

How can I make this work?


Solution

  • Try this and see if it works:

    os.rename(full_fname.encode('U8'), full_new_fname.encode('U8'))