Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's read and write add \x00 to the file

Tags:

python

file-io

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?

like image 786
Milo Wielondek Avatar asked Mar 15 '12 22:03

Milo Wielondek


1 Answers

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.

like image 176
kindall Avatar answered Sep 19 '22 10:09

kindall