Re: File write, weird behaviour

2023-02-19 Thread Thomas Passin
On 2/19/2023 6:10 PM, Mats Wichmann wrote: On 2/19/23 14:06, Dieter Maurer wrote: Azizbek Khamdamov wrote at 2023-2-19 19:03 +0500: ... Example 2 (weird behaviour) file = open("D:\Programming\Python\working_with_files\cities.txt", 'r+') ## contains list cities # the following code DOES NOT

Re: File write, weird behaviour

2023-02-19 Thread Mats Wichmann
On 2/19/23 14:06, Dieter Maurer wrote: Azizbek Khamdamov wrote at 2023-2-19 19:03 +0500: ... 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

Re: File write, weird behaviour

2023-02-19 Thread Dieter Maurer
Azizbek Khamdamov wrote at 2023-2-19 19:03 +0500: > ... >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

Re: File write, weird behaviour

2023-02-19 Thread Thomas Passin
On 2/19/2023 2:31 PM, Chris Angelico wrote: On Mon, 20 Feb 2023 at 06:24, Thomas Passin wrote: On 2/19/2023 1:53 PM, Chris Angelico wrote: On Mon, 20 Feb 2023 at 03:41, Azizbek Khamdamov wrote: Example 1 (works as expected) file =

Re: File write, weird behaviour

2023-02-19 Thread MRAB
On 2023-02-19 19:31, Chris Angelico wrote: On Mon, 20 Feb 2023 at 06:24, Thomas Passin wrote: On 2/19/2023 1:53 PM, Chris Angelico wrote: > On Mon, 20 Feb 2023 at 03:41, Azizbek Khamdamov > wrote: >> >> Example 1 (works as expected) >> >> file =

Re: File write, weird behaviour

2023-02-19 Thread Chris Angelico
On Mon, 20 Feb 2023 at 06:24, Thomas Passin wrote: > > On 2/19/2023 1:53 PM, Chris Angelico wrote: > > On Mon, 20 Feb 2023 at 03:41, Azizbek Khamdamov > > wrote: > >> > >> Example 1 (works as expected) > >> > >> file = open("D:\Programming\Python\working_with_files\cities.txt", > >> 'r+') ##

Re: File write, weird behaviour

2023-02-19 Thread Peter J. Holzer
On 2023-02-19 12:59:43 -0500, Thomas Passin wrote: > 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. > > > >

Re: File write, weird behaviour

2023-02-19 Thread Thomas Passin
On 2/19/2023 1:53 PM, Chris Angelico wrote: On Mon, 20 Feb 2023 at 03:41, Azizbek Khamdamov wrote: Example 1 (works as expected) file = open("D:\Programming\Python\working_with_files\cities.txt", 'r+') ## contains list cities Side note: You happened to get lucky with P, w, and c, but for

Re: File write, weird behaviour

2023-02-19 Thread Chris Angelico
On Mon, 20 Feb 2023 at 03:41, Azizbek Khamdamov wrote: > > Example 1 (works as expected) > > file = open("D:\Programming\Python\working_with_files\cities.txt", > 'r+') ## contains list cities Side note: You happened to get lucky with P, w, and c, but for the future, I recommend using forward

Re: File write, weird behaviour

2023-02-19 Thread Thomas Passin
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,

Re: File write, weird behaviour

2023-02-19 Thread MRAB
On 2023-02-19 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")

Re: File write, weird behaviour

2023-02-19 Thread Peter J. Holzer
On 2023-02-19 16:57:02 +, 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() Or

Re: File write, weird behaviour

2023-02-19 Thread Axy via Python-list
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() Can't deny, such a behaviour looks utterly weird. Try to avoid designing