Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnicodeDecodeError: 'cp932' codec can't decode byte 0xfc

import os

for root, dirs, files in os.walk('Path'):
     for file in files:
         if file.endswith('.c'):
             with open(os.path.join(root, file)) as f:
                    for line in f:
                        if 'word' in line:
                            print(line)

getting the error

UnicodeDecodeError: 'cp932' codec can't decode byte 0xfc in position 6616: illegal multibyte sequence

I think file needs shift jis encoding. can i set encoding at start only? i tried setting with open(os.path.join(root, file),'r',encoding='cp932') as f: but got same error

like image 921
Chetan.B Avatar asked Sep 01 '25 18:09

Chetan.B


2 Answers

You could pass errors='ignore', but make sure to check what is the encoding of your files.

open(os.path.join(root, file),'r', encoding='cp932', errors='ignore')
like image 89
cbodt Avatar answered Sep 04 '25 06:09

cbodt


Ended up here because I got the same error.

I'm just learning, but fortunately I found a solution.

If it says:

UnicodeDecodeError: 'cp932' codec can't decode

it means that the file that you are using is not encoded in cp932, so you actually need to change the encoding.

In my case, I was trying to read a file encoded in UTF-8, so the solution was to include that when I opened my file:

open("file.txt","r",encoding='utf-8')

I hope that this helps anybody who comes here because of the same error.

like image 20
Gabu Avatar answered Sep 04 '25 07:09

Gabu