http://stackoverflow.com/questions/125703/how-do-i-modify-a-text-file-in-python
#Unfortunately there is no way to insert into the middle of a file without re-writing it.
import sys
import fileinput
# replace all occurrences of 'sit' with 'SIT' and insert a line after the 5th
for i, line in enumerate(fileinput.input('C:/Users/test/Desktop/test.txt', inplace=1)):
sys.stdout.write(line.replace('sit', 'SIT')) # replace 'sit' and write
# if i == 4: sys.stdout.write('\n') # write a blank line after the 5th line
No comments:
Post a Comment