documentation on read.encode

Larry Martell larry.martell at gmail.com
Wed Jan 17 16:54:37 EST 2018


On Tue, Jan 16, 2018 at 3:58 PM, MRAB <python at mrabarnett.plus.com> wrote:
> On 2018-01-16 19:52, Larry Martell wrote:
>>
>> On Tue, Jan 16, 2018 at 2:35 PM, Gene Heskett <gheskett at shentel.net>
>> wrote:
>>>
>>> On Tuesday 16 January 2018 14:19:38 Larry Martell wrote:
>>>
>>>> On Tue, Jan 16, 2018 at 12:00 PM, Larry Martell
>>>
>>> <larry.martell at gmail.com> wrote:
>>>>
>>>> > Looking for 2.7 docs on read.encode - googling did not turn up
>>>> > anything.
>>>> >
>>>> > Specifically, looking for the supported options for base64, and how
>>>> > to specify them, e.g. Base64.NO_WRAP
>>>>
>>>> So I just realized that encode() is not a method of read() it's a
>>>> string method. But I still have the same question - can I pass in any
>>>> flags?
>>>>
>>>> My issue is that I am base64 encoding PNG images on linux and it's
>>>> putting a LF at the end of each line. If I do the same on Windows it's
>>>> putting CR/LF. I want the files to be encoded with no platform
>>>> dependences. Googling I found mention of Base64.NO_WRAP and I want to
>>>> pass that into encode() - can I do that?
>>>
>>>
>>> Di you not have the manpages installed?
>>>
>>> In my copy of the manpage:
>>> base64 [OPTION]... [FILE]
>>> where option is:
>>>  -w, --wrap=COLS
>>>               wrap encoded lines after COLS character (default 76).  Use
>>> 0 to disable line wrapping.
>>>
>>> Seems pretty simple.
>>
>>
>> But how do I use that in read().encode('base64')?
>>
> Use the base64 module instead, which is also how you would do it in Python
> 3.
>
> If you're getting CR/LF on Windows, that's because you're opening the file
> in text mode. In both Python 2 and Python 3 the base64 string will be a
> bytestring, which you'd write out to a file opened in binary mode.
>
> That's an extra bit of future-proofing! :-)

Thanks. That's what it ended up being. The code that was receiving the
PNG was not reading and writing the file as binary. Strangely that
worked on Linux but not on Windows.



More information about the Python-list mailing list