reading multiple files python . Im able to read a single text and encrypt it


Keywords:python 


Question: 

With the code below i'm able to encrypt a single file and decrypt it to the specified location. Now i wants to read multiple file as input and decrypt it to the destination. How can I achieve it.

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

Many thanks advance


1 Answer: 

As ShadowRanger pointed out, it's unclear what you are trying to achieve.

But generally for working with multiple files, you can use glog.glob function and just provide a mask for it as an input.

For example, here's a slightly modified code of yours which uses 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)

To encode all the .txt files, you would type *.txt:

Enter the mask for input files: *.txt

And then it will ask you for the output file names.

(Note that I incorporated your encryptText() function into the encryption() function. There is no need for two functions now.)