I have written a python script which read from a txt file and perform basic tasks like adding new line, deleting and editing existing lines. For deleting and editing, I load
The traditional solution is to copy the original file to a .sav
file and then write the new edited version to the original filename:
>>> import os
>>> filename = 'somefile.txt'
>>> with open(filename) as f:
lines = f.readlines()
>>> basename, ext = os.path.splitext(filename)
>>> savefile = basename + '.sav'
>>> os.rename(filename, savefile)
>>> lines = map(str.upper, lines) # do your edits here
>>> with open(filename, 'w') as f:
f.writelines(lines)