read and write the same text file

iMath redstone-cold at 163.com
Sat Mar 9 16:36:08 CET 2013


read and write the same text file 
Open a text file ,read the content ,then make some change on it ,then write it back to the file ,now the modified text  should only has the modified content but not the initial content ,so can we implement this by only set the mode parameter with open() function ?if yes ,what the parameter should be ?if no ,can we implement this by only one with statement ?
I implement this with 2 with statement as the following 

replace_pattern = re.compile(r"<.+?>",re.DOTALL)

def text_process(file):

    with open(file,'r') as f:
        text = f.read()

    with open(file,'w') as f:
        f.write(replace_pattern.sub('',text))



More information about the Python-list mailing list