Python : Add prefix and suffix to each line of a text

The below script add a prefix and a suffix to each line of a text. This script opens a notepad, you need to paste the text, and close the notepad after saving. The notepad opens again, automatically, showing the result. Works on windows with python 3.x

 

import subprocess
import fileinput
import sys

prefix = input('Enter the prefix ')
suffix = input('Enter the suffix ')
open('mytext.txt', 'w').close()
subprocess.call(['notepad.exe', 'mytext.txt'])
for line in fileinput.input('mytext.txt', inplace=True):
    line = line.rstrip()
    line = prefix + ' ' + line + ' ' + suffix + '\n'
    sys.stdout.write(line)
subprocess.call(['notepad.exe', 'mytext.txt'])

Comments