# caesar.py - Let's practice the Caesar cipher. # We need to read an input file one character at a time. # For each letter, let's add 3. # We need to make use of built-in functions bytearray() and chr() # in order to change character values. For example, # >>> chr(bytearray("pie")[0]+3) produces 's' # >>> chr(bytearray("pie")[1]+3) produces 'l' # >>> chr(bytearray("pie")[2]+3) produces 'h' inFile = open("plain.txt", "r") outFile = open("cipher.txt", "w") # The built-in read function can put the text of an entire file # into a variable. There is no need to read the file one # character at a time with read(1). text = inFile.read() # Loop to modify each character one by one. for i in range(0, len(text)): c = chr(bytearray(text)[i]+3) outFile.write(c) # Finally, tell Python we are done with the files. inFile.close() outFile.close()