UnicodeDecodeError when performing os.walk

前端 未结 6 1972
故里飘歌
故里飘歌 2020-12-05 14:30

I am getting the error:

\'ascii\' codec can\'t decode byte 0x8b in position 14: ordinal not in range(128)

when trying to do os.walk. The er

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 15:17

    \x8 is not a valid utf-8 encoding character. os.path expects the filenames to be in utf-8. If you want to access invalid filenames, you have to pass the os.path.walk the non-unicode startpath; this way the os module will not do the utf8 decoding. You would have to do it yourself and decide what to do with the filenames that contain incorrect characters.

    I.e.:

    for root, dirs, files in os.walk(startpath.encode('utf8')):
    

提交回复
热议问题