Renames files with special characters using os.rename()
python, rename
Solution
The problem is that your file contains non-ASCII characters, which cannot be represented correctly with ordinary Python (byte) strings. You can obtain the correct Unicode name from Python by passing a Unicode directory name to `listdir`; this will cause `listdir` to return Unicode filenames.
Example:
file_list = os.listdir(unicode(mydir)) # or e.g. mydir.decode('utf8')
os.rename(file_list[0], u'myfile.dbf')
Problem
In Python, I am trying to rename a number of files which names look like this: B-4¨íÐóµ(602).dbf Let's say this is the first element of the 'file_list' is my directory: ``` file_list = os.listdir(mydir) file_list[0] 'B-4???\xa8?\xed\x8d\xd0\x8f\xf3?\xb5(602).dbf' ``` Now this is quite different from the name of the file as seen in windows explorer (above), which is probably the root of the issue following: ``` os.rename(file_list[0],'myfile.dbf') WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect ``` Any ideas on how I could rename this file?