I have come across a weird problem when working with files in python. Let's say I have a text file and a simple piece of code that reads the contents of the file and then rewrites it with unaltered contents.
File.txt
This is a test file
Python code
f=open(File.txt,'r+')
data=f.read()
f.truncate(0)
f.write(data)
f.close()
After running the above code File.txt
seems to be the same. However, when I opened it in a hex editor I was surprised to see lots of \x00
(NULL) bytes before the actual contents of the text file, that wasn't there before.
Can anyone please explain?
Suppose your file has 20 bytes in it. So f.read()
reads 20 bytes. Now you truncate the file to 0 bytes. But your position-in-file pointer is still at 20. Why wouldn't it be? You haven't moved it. So when you write, you begin writing at the 21st byte. Your OS fills in the 20 missing bytes with zeroes.
To avoid this, f.seek(0)
before writing again.
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