UnicodeEncodeError indicates that you are trying to print file names. If he had a problem with os.lisdir() , you should see UnicodeDecodeError (Decode, not Encode).
Since you use the Unicode path name, os.listdir() returns easily decoded file names; on Windows, the file system uses UTF-16 to encode file names, and they are easily decoded in Python ( sys.getfilesystemencoding() tells Python which codec to use).
However, the Windows console uses a different encoding; in your case gbk , and this codec cannot display all the different characters that UTF-16 can encode.
Here you are looking for the print() statement. Perhaps you could use print(filename.encode('gbk', errors='replace')) to try and print the file names; unprintable characters will be replaced by a question mark.
Alternatively, you can use b'F:\\music' as a path and work with raw byte names instead of Unicode.
Martijn pieters
source share