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 follwed by the new "replaced" content. What can I do in order to delete the old stuff and only keep the new?
You need to use truncate if you want to do in place replace: https://docs.python.org/3/library/os.html?highlight=truncate#os.truncate
Or you use
open(myfile, 'w'). This will delete the old file an create a new one.
AFAIK truncate does not change the inode, but open(..., 'w') will create a new inode. But in most cases this does not matter. ... I tested it now. Both open(..., 'w') and truncate() don't change the inode number of the file. (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
truncate(), the solution could be
import re #open the xml file for reading: with open('path/test.xml','r+') as f: #convert to string: data = f.read() f.seek(0) f.write(re.sub(r"<string>ABC</string>(\s+)<string>(.*)</string>",r"<xyz>ABC</xyz>\1<xyz>\2</xyz>",data)) f.truncate()