Jason Plummer
Jason Plummer

Reputation: 11

How to display a files content in python

Here is my code so far. It prints out the encrypted text but won't print the text from the file before it is encrypted.

import string

char_set = string.printable[:-5]   
substitution_chars = char_set[-3:]+ char_set[:-3]    
encrypt_dict = {}

for i, k in enumerate (char_set):
    v = substitution_chars[i]
    encrypt_dict[k] = v

input_file=open('cryptopy_input.txt','r')    
text_list=list(input_file.read())

for i in range(len(text_list)):
    if text_list[i] in encrypt_dict:
        text_list[i]=encrypt_dict[text_list[i]]

output="".join(text_list)
output_file=open('cryptopy_output.txt','w')
output_file.write(output)
print(output)
output_file.close()

Upvotes: 1

Views: 343

Answers (1)

Sri
Sri

Reputation: 2318

In this line you are encrypting the text

text_list[i]=encrypt_dict[text_list[i]]

You are then appending that to output. And you are printing output. That is why what you are printing is already encrypted.

If you want to print the unencrypted text, print it before you encrypt it. Or store the encrypted text in a different variable.

Edit: I think your solution should look like this

for i in range(len(text_list)):
    print(text_list[i]
    if text_list[i] in encrypt_dict:
        text_list[i]=encrypt_dict[text_list[i]]

Upvotes: 1

Related Questions