Slow to check command line arg for e/d for every letter. Much better if we define the +/- operator as a lambda function once before looping through every letter.
import sys
# python3 caesar.py e 5 secret.txt secret.enc
# python3 caesar.py d 5 secret.enc secret.txt
if sys.argv[1]=='e': # better performance as oper function is set only once
oper = lambda a,b: a+b
elif sys.argv[1]=='d':
oper = lambda a,b: a-b
with open(sys.argv[3],'r') as inpf:
data=inpf.read()
newdata=''
for char in data:
''' # slow performance to check e/d for every letter in file
if sys.argv[1]=='e':
newchar=chr(ord(char) + int(sys.argv[2]))
elif sys.argv[1]=='d':
newchar=chr(ord(char) - int(sys.argv[2]))
'''
a = ord(char)
b = int(sys.argv[2]) #should be located before loop
newchar = chr( oper(a,b) )
newdata=newdata + newchar
with open(sys.argv[4],'w') as outf:
outf.write(newdata)
print('Finished')