正在读取多个文件。我可以阅读一个文本和加密我

2024-06-09 02:04:40 发布

您现在位置:Python中文网/ 问答频道 /正文

使用下面的代码,我可以加密单个文件并将其解密到指定的位置。现在我想读取多个文件作为输入,并将其解密到目标。我怎样才能做到

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()

非常感谢你提前


Tags: 文件ofthenameinputdefinfiledd
1条回答
网友
1楼 · 发布于 2024-06-09 02:04:40

正如暗影游侠所指出的,不清楚你想要达到什么目的

但一般来说,对于处理多个文件,可以使用glog.glob函数,只需为其提供一个掩码作为输入

例如,下面是您使用glob的稍微修改过的代码:

import os, glob

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):
    dtext = open(infile, 'r')
    dtext = dtext.readlines()
    print ('Input filename: ' + infile)

    # this was your encryptText(dtext) function
    outfile = raw_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()

if __name__=='__main__':
    mask = raw_input('Enter the mask for input files: ')
    for filename in glob.glob(mask):
        encryption(filename)

要对所有.txt文件进行编码,请键入*.txt:

Enter the mask for input files: *.txt

然后它会要求您输入输出文件名

(注意,我将encryptText()函数合并到encryption()函数中。现在不需要两个函数。)

相关问题 更多 >