'charmap' codec can't decode byte 0x9d in position 4837: character maps to <undefined> code example

Example 1: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 6148: character maps to

Add encoding:
	
	file = open(filename, encoding="utf8")

Example 2: UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 92: character maps to

#that's worked for me 

file = open(filepath,"r", encoding="utf8")

Example 3: 'charmap' codec can't decode byte 0x81 in pfd

#read file as rb mode

file_path = f"E:\Project\ezzytrace\media\Invoices\Fedex_invoices\{file_name}"
    with open(file_path,'rb' ) as f:
        file_data = f.read()
        response = HttpResponse(file_data, content_type='application/pdf')
        response['Content-Disposition'] = f'attachment; filename="{file_name}"'


    return  response

Tags:

Misc Example