On 18/05/2018 19:57, Chris Angelico wrote:
On Sat, May 19, 2018 at 4:48 AM, bartc <b...@freeuk.com> wrote:
The translation was straightforward, EXCEPT that I wasted an hour trying to
figure out to write /a single byte/ to a file. The following eventually
worked, using a binary file as a text one had Unicode problems, but it's
still hacky.

You can't write a single byte to a text file, because text files don't
store bytes. I'm not sure which part of this took you an hour to
figure out.

I've worked with text files for 40 years. Now Python is telling me I've been doing it wrong all that time!

Look at the original code I posted from which this Python was based. That creates a file - just a file - without worrying about whether it's text or binary. Files are just collections of bytes, as far as the OS is concerned.

So what could be more natural than writing a byte to the end of a file?

(Note that this particular file format is a hybrid; it has a text header followed by binary data. This is not unusual; probably every binary format will contain text too.

A programming language - and one that's supposed to be easy - should take that in its stride.)

# For Python 3 (it'll work on Python 2 but give the wrong results)

What does "work" mean? If it gives the wrong results, how is it working?

It works in that Python 2 is not complaining about anything, and it finishes (very quickly too). But the output file is 3 times the size it should be, and contains the wrong data.

end = 0                # lines containing 'end' can be removed

You're not writing Python code here.

Sorry but I'm lost without the block terminators. I needed them to match the logic to the original. After I used them, I decided it looked better.

--
bartc
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to