Im trying to add on to a project listed on https://www.thepythoncode.com/article/encrypt-decrypt-files-symmetric-python which is a text/file encryption setup with python, but every time I try to run the code, I hit the part of the code where it is actually encrypting the file and I'm getting the attribute error listed above
Full Error:
Traceback (most recent call last):
File "/Users/--------/Desktop/CODE/Python/TFEncrypter/TFEncrypter.py", line 38, in
<module>
encrypted_data = f.encrypt(file_data)
AttributeError: '_io.TextIOWrapper' object has no attribute 'encrypt'
Relevant Code:
""" IMPORTING AND DEFINITIONS """
import os
from cryptography.fernet import Fernet
def write_key():
key = Fernet.generate_key()
with open("key.tfe", "wb") as key_file:
key_file.write(key)
def load_key():
return open("key.tfe", "rb").read()
def make_file():
open("tte.txt", "x")
def encrypt(filename, key):
f = Fernet(key)
""" START OF PROGRAM """
path="key.tfe"
if os.path.isfile(path):
load_key()
task = input("Would You Like To Encrypt Or Decrypt A File?")
if type(task) == str:
if task == "Encrypt" or "encrypt":
task = input("Would You Like To Create A New File To Encrypt, Or Encrypt A Pre-Existing File (Note: Pre-Existing Files Must Be Named tte.txt) ANSWER AS: 'NEW FILE' or 'OLD FILE'")
if task == "NEW FILE":
path="tte.txt"
if os.path.isfile(path):
towrite = input("Text to encrypt in file:")
f = open("tte.txt", "w")
f.write(towrite)
with open("tte.txt", "rb") as file:
file_data = file.read()
encrypted_data = f.encrypt(file_data)
with open("encrypted.tfe", "wb") as file:
file.write(encrypted_data)
else:
make_file()
towrite = input("Text to encrypt in file:")
f = open("tte.txt", "w")
f.write(towrite)
with open("tte.txt", "rb") as file:
file_data = file.read()
encrypted_data = f.encrypt(file_data)
with open("encrypted.tfe", "wb") as file:
file.write(encrypted_data)