Issue
I am trying to create a program that copies a file, makes a cipher for the copied file and writes the ciphered text back into the copied file. I have tested the cipher and the final string and it works - the only issue I am running into is when I try to write over the file with the new string, the cipher is not applied to the file and nothing changes.
I have tried some code with f.flush, f.close, and have also attempted opening the file twice (in read to get the string, and then opening it in write to write it) and nothing has worked. I am working in Jupyter Notebook, but have also imported the same code to IntelliJ and nothing changed.
import random
import os
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
cipher = {"A": [], "B": [], "C": [], "D": [], "E": [], "F": [], "G": [], "H": [], "I": [], "J": [], "K": [], "L": [], "M": [], "N": [], "O": [], "P": [], "Q": [], "R": [], "S": [], "T": [], "U": [], "V": [], "W": [], "X": [], "Y": [], "Z": []}
random_alphabet = random.sample(alphabet, 26)
for letter in range(26):
cipher[alphabet[letter]] = random_alphabet[letter]
os.popen('copy RomeoAndJuliet.txt SecretRomeoAndJuliet.txt') #copies original file
with open("SecretRomeoAndJuliet.txt",'r+', encoding="UTF-8") as f: #opens new file with read&write
string = ""
for line in f:
newline = ""
for letter in line:
for a in cipher:
success = False
if letter.upper() == cipher[a]:
newline = newline + a
success = True
break
if not success:
newline = newline + letter
string = string + newline
f.write(string)
f.close()
Here is the code that I've been working with, is there any way to write the final string into the file? What I have tried does not seem to work. Thank you for any suggestions!
Solution
To be honest, what I believe to be the issue with your code is that you are using f.write(string) after reading from f. When you read from the file, the file pointer is positioned at the end of the file, so when you try to write, it appends the content to the end of the file without ciphering the content.
You can either try to close and reopen the file in write mode or use f.seek(0) to move the file pointer to the beginning of the file before writing to it.
This should be the solution to your problem, tell me if it does not work:
import random
import os
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
cipher = {"A": [], "B": [], "C": [], "D": [], "E": [], "F": [], "G": [], "H": [], "I": [], "J": [], "K": [], "L": [], "M": [], "N": [], "O": [], "P": [], "Q": [], "R": [], "S": [], "T": [], "U": [], "V": [], "W": [], "X": [], "Y": [], "Z": []}
random_alphabet = random.sample(alphabet, 26)
for letter in range(26):
cipher[alphabet[letter]] = random_alphabet[letter]
os.popen('copy RomeoAndJuliet.txt SecretRomeoAndJuliet.txt')
with open("SecretRomeoAndJuliet.txt", 'r+', encoding="UTF-8") as f:
string = ""
for line in f:
newline = ""
for letter in line:
for a in cipher:
success = False
if letter.upper() == cipher[a]:
newline = newline + a
success = True
break
if not success:
newline = newline + letter
string = string + newline
f.seek(0)
f.write(string)
f.truncate()
Answered By - Lashen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.