Assume there is a file test.txt
containing a string 'test'
.
Now, consider the following Python code:
f = open('test', 'r+')
f.read()
f.truncate(0)
f.write('passed')
f.flush();
Now I expect test.txt
to contain 'passed'
now, however there are additionally some strange symbols!
Update: flush after truncate does not help.
Yeah, that's true that truncate()
doesn't move the position, but said that, is simple as death:
f.read()
f.seek(0)
f.truncate(0)
f.close()
this is perfectly working ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With