When I run my code that encrypts a file, I get the following error:
<_io.TextIOWrapper name='C:/Users/depen/OneDrive/Documents/PNG_transparency_demonstration_1 >(2).png' mode='r' encoding='cp1252'>
read
encrypting
encrypted
encrypted file saved
Traceback (most recent call last):
File "c:\Users\depen\OneDrive\Documents\python\timer\encrypt.py", line 93, in <module>
encrypt(fpath)
File "c:\Users\depen\OneDrive\Documents\python\timer\encrypt.py", line 67, in encrypt
os.rename(file_fpath, file_fpath + ".encrypted")
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:/Users/depen/OneDrive/Documents/PNG_transparency_demonstration_1 (2).png' -> 'C:/Users/depen/OneDrive/Documents/PNG_transparency_demonstration_1 (2).png.encrypted'
Here is my code:
fernet = AES(key)
def encrypt(file_path):
#start a timer
start = time.time()
def read_in_chunks(file_object, chunk_size= 52428800):
while True:
data = file_object.read(chunk_size)
if not data:
break
yield data
with open(file_path, "rb") as f:
for piece in read_in_chunks(f):
data = fernet.encrypt(randbytes(16), piece, randbytes(16))
currenttime = time.time()
print(currenttime - start)
with open (file_path + ".enc", "ab") as f:
f.write(data)
f.close()
with open("filenames.txt", "a") as file:
file.write(file_path + "\n")
file.close()
#end the timer
end = time.time()
print ("Time taken to encrypt `: " + str(end - start))
os.remove(file_path)
The code does encrypt the file but dose not add a .encrypted to the end of the filename.