Skip to content
Browse files

Pylint-suggested corrections. Several bug fixes.

  • Loading branch information...
1 parent 91f8abb commit 0065e759ef77057c7d4e719558fbd13331bf7772 @asweigart committed
Showing with 54 additions and 0 deletions.
  1. +54 −0 transpositionFileCipher.py
View
54 transpositionFileCipher.py
@@ -0,0 +1,54 @@
+# Transposition Cipher Encrypt File
+# http://inventwithpython.com/codebreaker (BSD Licensed)
+
+import time, os, sys, transpositionEncrypt, transpositionDecrypt
+
+def main():
+ inputFilename = 'frankenstein.txt'
+ # BE CAREFUL! If a file with the outputFilename name already exists, this
+ # program will overwrite that file.
+ outputFilename = 'frankenstein.encrypted.txt'
+ myKey = 10
+ myMode = 'encrypt' # set to 'encrypt' or 'decrypt'
+
+ # If the input file does not exist, then the program terminates early on.
+ if not os.path.exists(inputFilename):
+ print('The file %s does not exist. Quitting...' % (inputFilename))
+ sys.exit()
+
+ # If the output file already exists, give the user a chance to quit.
+ if os.path.exists(outputFilename):
+ print('This will overwrite the file %s. (C)ontinue or (Q)uit?' % (outputFilename))
+ response = input('> ')
+ if not response.lower().startswith('c'):
+ sys.exit()
+
+ # Read in the message from the input file
+ fileObj = open(inputFilename)
+ content = fileObj.read()
+ fileObj.close()
+
+ print('%sing...' % (myMode.title()))
+
+ # Measure how long the encryption takes.
+ startTime = time.time()
+ if myMode == 'encrypt':
+ translated = transpositionEncrypt.encryptMessage(myKey, content)
+ elif myMode == 'decrypt':
+ translated = transpositionDecrypt.decryptMessage(myKey, content)
+ totalTime = round(time.time() - startTime, 2)
+ print('%sion time: %s seconds' % (myMode.title(), totalTime))
+
+ # Write out the translated message to the output file.
+ outputFileObj = open(outputFilename, 'w')
+ outputFileObj.write(translated)
+ outputFileObj.close()
+
+ print('Done %sing %s (%s characters).' % (myMode, inputFilename, len(content)))
+ print('%sed file is %s.' % (myMode.title(), outputFilename))
+
+
+# If transpositionCipherFile.py is run (instead of imported as a module) call
+# the main() function.
+if __name__ == '__main__':
+ main()

0 comments on commit 0065e75

Please sign in to comment.
Something went wrong with that request. Please try again.