UnicodeDecodeError when performing os.walk

前端 未结 6 1981
故里飘歌
故里飘歌 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:20

    I got this problem when use os.walk on some directories with Chinese (unicode) names. I implemented the walk function myself as follows, which worked fine with unicode dir/file names.

    import os
    
    ft = list(tuple())
    
    def walk(dir, cur):
        fl = os.listdir(dir)
        for f in fl:
            full_path = os.path.join(dir,f)    
            if os.path.isdir(full_path):
                walk(full_path, cur)
            else:
                path, filename = full_path.rsplit('/',1)
                ft.append((path, filename, os.path.getsize(full_path)))
    

提交回复
热议问题