I have the following code:
import re
#open the xml file for reading:
file = open('path/test.xml','r+')
#convert to string:
data = file.read()
file.write(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>",r"<xyz>ABC</xyz>\1<xyz>\2</xyz>",data))
file.close()
where I’d like to replace the old content that’s in the file with the new content. However, when I execute my code, the file "test.xml" is appended, i.e. I have the old content followed by the new "replaced" content. What can I do in order to delete the old stuff and only keep the new?
You need
seekto the beginning of the file before writing and then usefile.truncate()if you want to do inplace replace:The other way is to read the file then open it again with
open(myfile, 'w'):Neither
truncatenoropen(..., 'w')will change the inode number of the file (I tested twice, once with Ubuntu 12.04 NFS and once with ext4).By the way, this is not really related to Python. The interpreter calls the corresponding low level API. The method
truncate()works the same in the C programming language: See http://man7.org/linux/man-pages/man2/truncate.2.html