'double encoding through cp1252 and base 64

From a client I am getting a pdf file, which is encoded in cp 1252 and for transfer is also encoded in base 64. Till now a shell program returns the file into the original form through this code line: output= [System.Text.Encoding]::GetEncoding(1252).GetString([System.Convert]::FromBase64String(input)) and this works.

Now I am implementing a python version, to supersede this implementation. This looks generally like this:

enc_file = read_from_txt.open_file(location_of_file)
plain_file= base64.b64decode(enc_file)
with open('filename', 'w') as writer:
    writer.write(plain_file.decode('cp1252'))

where read_from_txt.open_file just does this:

with open(file_location, 'rb') as fileReader:
    read = fileReader.read()
    return read

But for some reason, I am getting an error in the plain_file.decode('cp1252'), where it can not decode a line in the file. From what I am understanding though, the python program should do exactly the same, as the powershell does.

Concrete error is: UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 188: character maps to undefined

Any help is appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source