【发布时间】:2020-03-19 16:53:48
【问题描述】:
我正在尝试创建一个程序来允许用户加密或解密文件。我只是不知道下一步该做什么,另一个问题是当我按下回车键时退出程序并不总是有效。这些是我必须使用的功能。
def main():
menuSelection = displayMenuAndGetOption()
if menuSelection == "Q":
input("\nRun complete. Press the Enter key to exit.")
elif menuSelection == "E":
file = getFiles(menuSelection)
elif menuSelection == "D":
file = getFiles(menuSelection)
else:
print("\nError - invalid option.")
input("\nRun complete. Press the Enter key to exit.")
def displayMenuAndGetOption():
print("\nFile Encryption Program")
print("\nE = Encrypt a file", "D = Decrypt a file", "Q = Quit the Program", sep = "\n")
option = input("\nEnter menu selection (E, D, or Q): ").upper()
return option
def getFiles(fileOption):
while fileOption != "":
try:
if fileOption == "E":
inputFile = input("\nEnter the file to ENCRYPT. Press Enter alone to abort: ")
filetext = open(inputFile, "r")
elif fileOption == "D":
inputFile = input("\nEnter the file to DECRYPT. Press Enter alone to abort: ")
filetext = open(inputFile, "r")
except IOError:
print("Error - that file does not exist. Try again.")
else:
newfile = input("Enter the output file name: ")
outputFile = open(newfile, "w")
fileOption
return fileOption
def convert(inputFile, outputFile):
# Encryption and decryption are inverse of one another
CODE = {'A':')','a':'0','B':'(','b':'9','C':'*','c':'8',\
'D':'&','d':'7','E':'^','e':'6','F':'%','f':'5',\
'G':'$','g':'4','H':'#','h':'3','I':'@','i':'2',\
'J':'!','j':'1','K':'Z','k':'z','L':'Y','l':'y',\
'M':'X','m':'x','N':'W','n':'w','O':'V','o':'v',\
'P':'U','p':'u','Q':'T','q':'t','R':'S','r':'s',\
'S':'R','s':'r','T':'Q','t':'q','U':'P','u':'p',\
'V':'O','v':'o','W':'N','w':'n','X':'M','x':'m',\
'Y':'L','y':'l','Z':'K','z':'k','!':'J','1':'j',\
'@':'I','2':'i','#':'H','3':'h','$':'G','4':'g',\
'%':'F','5':'f','^':'E','6':'e','&':'D','7':'d',\
'*':'C','8':'c','(':'B','9':'b',')':'A','0':'a',\
':':',',',':':','?':'.','.':'?','<':'>','>':'<',\
"'":'"','"':"'",'+':'-','-':'+','=':';',';':'=',\
'{':'[','[':'{','}':']',']':'}'}
result = ''
fileText = inputFile.read()
inputFile.close()
for eachchar in fileText:
returnVal = CODE.get(eachchar,eachchar)
result = result + returnVal
outputFile.write(result)
outputFile.close()
这就是它的样子。
样品运行
File Encryption Program
E = Encrypt a file
D = Decrypt a file
Q = Quit the program
Enter menu selection (E, D, or Q): x
Error - Invalid option.
Run complete. Press the Enter key to exit.
File Encryption Program
E = Encrypt a file
D = Decrypt a file
Q = Quit the program
Enter menu selection (E, D, or Q): e
Enter the file to ENCRYPT. Press Enter alone to abort: filedoesnotexist.txt
Error - that file does not exist. Try again.
Enter the file to ENCRYPT. Press Enter alone to abort: filetoencrypt.txt
Enter the output file name: encryptedfile.txt
Run complete. Press the Enter key to exit.
File Encryption Program
E = Encrypt a file
D = Decrypt a file
Q = Quit the program
Enter menu selection (E, D, or Q): d
Enter the file to DECRYPT. Press Enter alone to abort: encryptedfile.txt
Enter the output file name: decryptedfile.txt
Run complete. Press the Enter key to exit.
【问题讨论】:
-
在 Python 中,变量和函数被命名为
lowercase_with_underscores而不是camelCase -
使用上下文管理器来处理文件。我会认真考虑一些重构。例如,没有理由让您的加密函数 (
convert) 处理文件。明天我会尝试发布你的程序的重构版本。
标签: python file dictionary exception encryption