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?
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()
On 19/02/2023 14:03, Azizbek Khamdamov wrote:
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")
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
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?
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?
Example 1 (works as expected)
file = open("D:\Programming\Python\working_with_files\cities.txt",
'r+') ## contains list cities
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."
file.close()
On Mon, 20 Feb 2023 at 06:24, Thomas Passin <list1@tompassin.net> wrote:
On 2/19/2023 1:53 PM, Chris Angelico wrote:
On Mon, 20 Feb 2023 at 03:41, Azizbek Khamdamov
<azizbek.khamdamov@gmail.com> 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 slashes for your paths:
open("D:/Programming/Python/working_with_files/cities.txt", "r+")
Otherwise, you may run into annoying and hard-to-solve problems. Or
alternatively, you'll upgrade to a newer Python and start getting
warnings, which would at least tell you that there's a problem.
Or use r'...' strings. If you are copying a path to clipboard from
Windows Explorer - a fairly common operation - it's much easier to
prepend the "r" than to change all the backslashes to forward slashes.
Yep, either way. I personally prefer using forward slashes since
there's no way to end an r-string with a single backslash, which is
often useful when building a path:
# won't work
path = r"c:\path\to\some\"
open(path + "file")
# will work
path = "c:/path/to/some/"
open(path + "file")
On 2/19/2023 1:53 PM, Chris Angelico wrote:
On Mon, 20 Feb 2023 at 03:41, Azizbek Khamdamov <azizbek.khamdamov@gmail.com> 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 slashes for your paths:
open("D:/Programming/Python/working_with_files/cities.txt", "r+")
Otherwise, you may run into annoying and hard-to-solve problems. Or alternatively, you'll upgrade to a newer Python and start getting
warnings, which would at least tell you that there's a problem.
Or use r'...' strings. If you are copying a path to clipboard from
Windows Explorer - a fairly common operation - it's much easier to
prepend the "r" than to change all the backslashes to forward slashes.
On Mon, 20 Feb 2023 at 03:41, Azizbek Khamdamov
<azizbek.khamdamov@gmail.com> 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 slashes for your paths:
open("D:/Programming/Python/working_with_files/cities.txt", "r+")
Otherwise, you may run into annoying and hard-to-solve problems. Or alternatively, you'll upgrade to a newer Python and start getting
warnings, which would at least tell you that there's a problem.
On Mon, 20 Feb 2023 at 06:24, Thomas Passin <list1@tompassin.net> wrote:
On 2/19/2023 1:53 PM, Chris Angelico wrote:
On Mon, 20 Feb 2023 at 03:41, Azizbek Khamdamov
<azizbek.khamdamov@gmail.com> 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 slashes for your paths:
open("D:/Programming/Python/working_with_files/cities.txt", "r+")
Otherwise, you may run into annoying and hard-to-solve problems. Or
alternatively, you'll upgrade to a newer Python and start getting
warnings, which would at least tell you that there's a problem.
Or use r'...' strings. If you are copying a path to clipboard from
Windows Explorer - a fairly common operation - it's much easier to
prepend the "r" than to change all the backslashes to forward slashes.
Yep, either way. I personally prefer using forward slashes since
there's no way to end an r-string with a single backslash, which is
often useful when building a path:
# won't work
path = r"c:\path\to\some\"
open(path + "file")
# will work
path = "c:/path/to/some/"
open(path + "file")
PureWindowsPath('c:/this/is/a/test')from pathlib import PurePath
p1 = PurePath('c:/this') / 'is' / 'a' /'test'
p1
PureWindowsPath('c:/this/is/a/test')p2 = PurePath('c:/this/') /'is' /'a' /'test'
p2
...
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?
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 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?
The effect of "r+" (and friends) is specified by the C standard.
The Linux doc (of `fopen`) tells us that ANSI C requires that
a file positioning command (e.g. `seek`) must intervene
between input and output operations. Your example above violates
this condition. Therefore, weird behavior is to be expected.
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 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?
The effect of "r+" (and friends) is specified by the C standard.
The Linux doc (of `fopen`) tells us that ANSI C requires that
a file positioning command (e.g. `seek`) must intervene
between input and output operations. Your example above violates
this condition. Therefore, weird behavior is to be expected.
If this isn't sufficiently described, someone should raise an issue
against the Python docs. I know that many concepts are "inherited from" environments generally in the POSIX space and the C language, because
that's where Python was hatched (all of which makes perfect sense to me, who's been working in those spaces for...ever), but a Python programmer shouldn't have to read the ISO C standard (which is not free, although
you can find copies on-line), or the POSIX standard (which also is not
free, though manpages for systems like Linux cover the same material),
in order to figure out how Python is going to work.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 02:04:56 |
Calls: | 10,385 |
Calls today: | 2 |
Files: | 14,057 |
Messages: | 6,416,581 |