简体   繁体   中英

Getting error on windows but not on linux with japanese characters

I want to read files names from a directory and print out their names. This is my code:

png_files = []
for root, dirs, files in os.walk('output'):
    for file in files:
        png_files.append(file)
for name in png_files:
    print(name)

This works but only on linux (not on windows)and I dont know why. My files have some Japanese characters and I know that the problem is in them but dont know how to fix.

I am getting this message:

'charmap' codec can't encode characters in position 19-27: character maps to <undefined>

I have tried encode('UTF-8') and then I get :

b'forecast_traffic\\xe4\\xb8'

I know that when you have b'' it means it is a bytestring so i tried name.encode('UTF-8').decode() but i again get the same error message and if I try just encode i get the error :

'str' object has no attribute 'decode'

Does anybody know how to fix this and where my mistake is?

"Windows console doesn't print Unicode" bug was fixed. Check the link below. http://bugs.python.org/issue1602 and this https://wiki.python.org/moin/PrintFails

OR install win_unicode_console using pip install win-unicode-console

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM