Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Heisenberg might have been here.


devel / comp.lang.python / Re: File write, weird behaviour

SubjectAuthor
o Re: File write, weird behaviourThomas Passin

1
Re: File write, weird behaviour

<mailman.1885.1676829597.20444.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21763&group=comp.lang.python#21763

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: lis...@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: File write, weird behaviour
Date: Sun, 19 Feb 2023 12:59:43 -0500
Lines: 57
Message-ID: <mailman.1885.1676829597.20444.python-list@python.org>
References: <CAN14BN5=0sL-d8HQ4hyAYLicvGP4TzhbR=WEr6Ev-tV2TPJqWQ@mail.gmail.com>
<e0d474c3-c792-0367-207c-20d5d1a9295d@declassed.art>
<6458f9a9-7b87-4e56-9db0-298a805d9f63@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de kTtMrGFBruF1o3Ibl7xDcQnF2HXBFB1OqQUez3kjks0g==
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=oeKH7JI2;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.004
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'looks': 0.02;
'received:23.83.212': 0.09; 'received:elm.relay.mailchannels.net':
0.09; '(i.e.,': 0.16; 'arguments': 0.16; 'behaviour': 0.16;
'disk,': 0.16; 'flag': 0.16; 'possibility,': 0.16;
'received:10.0.0': 0.16; 'received:64.90': 0.16;
'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'received:dreamhost.com': 0.16; 'subject:File': 0.16; 'wrote:':
0.16; 'python': 0.16; "can't": 0.17; 'to:addr:python-list': 0.20;
'written': 0.22; 'way.': 0.22; 'code': 0.23; 'anything': 0.25;
'actual': 0.25; 'cities': 0.26; 'example,': 0.28; 'header:User-
Agent:1': 0.30; 'takes': 0.31; 'am,': 0.31; 'program': 0.31;
'(as': 0.32; 'keyword': 0.32; 'python-list': 0.32; 'realize':
0.32; 'received:10.0': 0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'but': 0.32; 'package':
0.34; 'header:In-Reply-To:1': 0.34; 'following': 0.35; 'files':
0.36; 'using': 0.37; 'file': 0.38; 'could': 0.38; 'added': 0.39;
'list': 0.39; 'beginning': 0.40; 'explain': 0.40; 'try': 0.40;
'should': 0.40; 'skip:b 10': 0.63; 'your': 0.64;
'header:Received:6': 0.67; 'received:64': 0.67; 'adds': 0.69;
'content': 0.72
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1676829585; a=rsa-sha256;
cv=none;
b=uF+s0xeFRfNvMV0BM4bmWpRLqAY8RAhCE33viM7/KJBE6Fmj6LSceoI1dI8WHMrqhiL6l/
EqTf6gbOzYYQu1R829iut7hRVP/rkWbOQTmhSXFTTLJDwg2RFq8ADAGidY+K+ukKQwcQA+
Z66O3aMygM8izwx44GnPhbQ2Czi2Ks6QB6xmYnKxwNSXDwMP6QJ+GT9PSk2Q635iweN9gU
sb0jLfeUQS7k3RfaB0MuOeafR2rkaJNUHk1EKyh4oTqZfXN6eo/upIAMT4GdPEsN5kVO3f
fqqxuWM6U+Xw6SdJbZQ1kFoGHXr/xuwnNlc7fWVt57fXV0KpJKxDWnoX5EikWA==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1676829585;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=z1Stce3oBM+DZ5tORVjDizxkCcS/1uQHAXR+sJ5j9PY=;
b=0Ux9/XYTyOe5DDuQffo0NUZhjlP84h9IwQQrYD7Eg41XamM3QbpHiLjHFLzKPv+Q4JH148
HUAcUq1sMQCpQa1JttHuP58JEnxJiBha3ZrvqOrnWWq3HoPr7nX79hBETRjjgK2eX4oUj+
e2hc3KuS0pCM8a3pN+nMM1/Ia0InDATjdfThqjKfCOZ0mW3t39UBmlA7ycIROfUME5KzVt
8n4t2pswZFM6Dx9oLy5hWjuRdsDnWLJtW9PAypF0Rkvh6H2AvSE+mHm+pWe8goNj+OcVvF
UE5KI7npscJPDyMJ05OEQfkbgmfEdABaTW4BHwSaGBvpKBtcfhacjIx0+gLxsQ==
ARC-Authentication-Results: i=1; rspamd-9788b98bc-qb2vt;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Industry-Befitting: 6a50a78e0196123c_1676829585819_3102726037
X-MC-Loop-Signature: 1676829585819:2715495685
X-MC-Ingress-Time: 1676829585819
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1676829585;
bh=z1Stce3oBM+DZ5tORVjDizxkCcS/1uQHAXR+sJ5j9PY=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=oeKH7JI2xtJPgPrcgscaxWYCfhzq744K7WqAz4eM9pKEopi6BvNjBZdWDc3elr+uk
aJU5DbZdRK/nSyEIa50CLZpgeLZxeI5yjeukiO9QxwYL/Vphrt+nLNbEXyxWYR5JTq
se65bDJ/6SqOVEl+kGKH2toZYAOw7GxSzGEVX6gvhUqzCu30S0mtRDX6tKmaTiMKaN
XzEwV6qqmAewFGr01y4CpNCf/byDXKJeEY1xhF+9xxKceRZtXvda/HNPk+7hCobe50
EP+1JH1lN88Kwa+Gub7Xn58SeBEAi/8AoxxQBJHTLEhcxVFAAXxfiKSkrQSqCidMqI
cUpjdn1hBZTdA==
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.8.0
Content-Language: en-US
In-Reply-To: <e0d474c3-c792-0367-207c-20d5d1a9295d@declassed.art>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <6458f9a9-7b87-4e56-9db0-298a805d9f63@tompassin.net>
X-Mailman-Original-References: <CAN14BN5=0sL-d8HQ4hyAYLicvGP4TzhbR=WEr6Ev-tV2TPJqWQ@mail.gmail.com>
<e0d474c3-c792-0367-207c-20d5d1a9295d@declassed.art>
 by: Thomas Passin - Sun, 19 Feb 2023 17:59 UTC

On 2/19/2023 11:57 AM, Axy via Python-list wrote:
> Looks like the data to be written is buffered, so actual write takes
> place after readlines(), when close() flushes buffers.
>
> See io package documentation, BufferedIOBase.
>
> The solution is file.flush() after file.write()

Another possibility, from the Python docs:

"...TextIOWrapper (i.e., files opened with mode='r+') ... To disable
buffering in TextIOWrapper, consider using the write_through flag for
io.TextIOWrapper.reconfigure()"

Also from the docs:

"Warning: Calling f.write() without using the with keyword or calling
f.close() might result in the arguments of f.write() not being
completely written to the disk, even if the program exits successfully."

I realize that in the example, close() was called ... but not
immediately after the write().

> Can't deny, such a behaviour looks utterly weird. Try to avoid designing
> your software this way.
>
> Axy.
>
> On 19/02/2023 14:03, Azizbek Khamdamov wrote:
>> Example 1 (works as expected)
>>
>> file = open("D:\Programming\Python\working_with_files\cities.txt",
>> 'r+') ## contains list cities
>> # the following code adds new record to the beginning of the file,
>> expected behaviour
>> file.write("new city\n")
>>
>> file.close()
>>
>>
>> Example 2 (weird behaviour)
>>
>> file = open("D:\Programming\Python\working_with_files\cities.txt",
>> 'r+') ## contains list cities
>> # the following code DOES NOT add new record TO THE BEGINNING of the
>> file IF FOLLOWED BY readline() and readlines()# Expected behaviour:
>> new content should be added to the beginning of the file (as in
>> Example 1)
>> file.write("new city\n")
>>
>> file.readlines()
>> file.close()
>>
>> I could not find anything in documentation to explain this strange
>> behaviour. Why is this happening?

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor