Error in base64 module

Fredrik Lundh effbot at
Sat Sep 2 12:39:01 CEST 2000

jun won, Seo wrote:
> I have a string "xde9usauwd8"
> I want to base64decoding that string "xde9usauwd8".

base64 means that 4-character ascii string chunks are
decoded into 3-byte binary strings.

> binascii.Error: Incorrect padding
> Why?..
> Error in Python's base64 module?


unlike PHP3, Python doesn't ignore errors if it can detect
them, and an 11-character base64 string isn't valid.

if you cannot get the sender to give you valid data, you
can pad the string yourself before decoding it:

    def fixup(s):
        # add proper padding to a base64 string
        n = len(s) & 3
        if n:
            s = s + "="*(4-n)
        return s

    >>> base64.decodestring(fixup("xde9usauwd8"))


<!-- (the eff-bot guide to) the standard python library:

More information about the Python-list mailing list