[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic
From: |
Markus Armbruster |
Subject: |
Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic |
Date: |
Mon, 28 Sep 2020 14:09:12 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) |
Eric Blake <eblake@redhat.com> writes:
> On 9/25/20 8:52 AM, Markus Armbruster wrote:
>
>>>> This was my best attempt to open the file read/write, creating it if it
>>>> doesn't exist.
>>>>
>>>> Plain
>>>>
>>>> f = open(pathname, "r+", encoding='utf-8')
>>>>
>>>> fails instead of creates, and
>>>>
>>>> f = open(pathname, "w+", encoding='utf-8')
>>>>
>>>> truncates.
>>>>
>>>> If you know a better way, tell me!
>>>
>>> IIUC, you need "a+" as the mode, rather than "w+"
>> Sure this lets me do
>> f.seek(0)
>> f.truncate(0)
>> f.write(text)
>> to overwrite the old contents on all systems?
>
> As long as you do a single pass over the output (you issue a stream of
> f.write() after the truncate, but never a seek), then this will work.
Well, I do seek(), right before the truncate.
>> Documentation cautions:
>> [...] 'a' for appending (which on some Unix systems, means that
>> all
>> writes append to the end of the file regardless of the current seek
>> position).
>
> Yes, that means that random access is impossible on such a stream.
> But not all file creation patterns require random access.
To be honest, I still prefer the code I wrote, because there the reader
only wonders why I didn't just open(), while here we get to argue about
subtleties of mode "a+".
- Re: [PATCH v2 25/38] qapi/gen.py: add type hint annotations, (continued)
- [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, John Snow, 2020/09/22
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, Eduardo Habkost, 2020/09/23
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, John Snow, 2020/09/23
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, Cleber Rosa, 2020/09/24
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, Markus Armbruster, 2020/09/25
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, Daniel P . Berrangé, 2020/09/25
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, Eric Blake, 2020/09/25
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, Markus Armbruster, 2020/09/25
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, Eric Blake, 2020/09/25
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic,
Markus Armbruster <=
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, John Snow, 2020/09/28
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, Eduardo Habkost, 2020/09/25
- Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, John Snow, 2020/09/25
Re: [PATCH v2 28/38] qapi/gen.py: update write() to be more idiomatic, Cleber Rosa, 2020/09/24
[PATCH v2 29/38] qapi/gen.py: delint with pylint, John Snow, 2020/09/22
[PATCH v2 10/38] qapi/common.py: delint with pylint, John Snow, 2020/09/22