I
iMath
read and write the same text file
Open a text file ,read the content ,then make some change on it ,then writeit 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))
Open a text file ,read the content ,then make some change on it ,then writeit 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))