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?