Python replace and overwrite instead of appending

2019-02-11 11:20发布

问题:

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?

回答1:

You need to use truncate if you want to do in place replace: http://docs.python.org/library/stdtypes.html?highlight=truncate#file.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



回答2:

Using 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()


回答3:

file='path/test.xml' 
with open(file, 'w') as filetowrite:
    filetowrite.write('new content')
    filetowrite.close()

Open the file in 'w' mode , you will be able to replace its current text save the file with new contents.



回答4:

import os#must import this library
if os.path.exists('TwitterDB.csv'):
        os.remove('TwitterDB.csv') #this deletes the file
else:
        print("The file does not exist")#add this to prevent errors

I had a similar problem, and instead of overwriting my existing file using the different 'modes', I just deleted the file before using it again, so that it would be as if I was appending to a new file on each run of my code.