使用下面的代码,我可以加密单个文件并将其解密到指定的位置。现在我想读取多个文件作为输入,并将其解密到目标。我怎样才能做到
import os
dd = {'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',
':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<',
"'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=',
'{': '[', '[': '{', '}': ']', ']': '}'}
def encryption():
infile = str(input('Enter the name of the input file: '))
dtext = open(infile, 'r')
dtext = dtext.readlines()
encryptText(dtext)
def encryptText(dtext):
outfile = str(input('Enter the name of the output file: '))
etext = open(outfile, 'w')
for line in dtext:
for cword in line:
encrypted = (dd.get(cword, cword))
etext.write(encrypted)
etext.close()
encryption()
非常感谢你提前
正如暗影游侠所指出的,不清楚你想要达到什么目的
但一般来说,对于处理多个文件,可以使用
glog.glob
函数,只需为其提供一个掩码作为输入例如,下面是您使用glob的稍微修改过的代码:
要对所有.txt文件进行编码,请键入*.txt:
然后它会要求您输入输出文件名
(注意,我将encryptText()函数合并到encryption()函数中。现在不需要两个函数。)
相关问题 更多 >
编程相关推荐