首页 > 解决方案 > FileNotFoundError: [Errno 2] No such file or directory: 'keys_keys.txt'

问题描述

As I'm working with files and cryptography library, I'm have problem reading file. Details are as follows,

Getting following exception:

Traceback (most recent call last):
  File "F:/PyCharm Python Works/OpenCity/main_directory/main_file.py", line 8, in <module>
    fe.encrypt_file(z2[0], z2[1])
  File "F:\PyCharm Python Works\OpenCity\cryptograph\file_encryptor.py", line 4, in encrypt_file
    keys = kr.read_keys()
  File "F:\PyCharm Python Works\OpenCity\cryptograph\key_reader.py", line 3, in read_keys
    f1 = open('keys.key', 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'keys.key'

cryptograph file_decryptor.py:

def decrypt_file(x, y):

    from cryptograph import key_reader as kr
    from cryptography.fernet import Fernet
    keys = kr.read_keys()
    key = keys[0]
    input_file = x
    output_file = y

    with open(input_file, 'rb') as f:
        data = f.read()

    f1 = Fernet(key)
    encrypted = f1.decrypt(data)

    with open(output_file, 'wb') as f:
        f.write(encrypted)

file_encryptor.py:

def encrypt_file(x, y):
    from cryptograph import key_reader as kr
    from cryptography.fernet import Fernet
    keys = kr.read_keys()
    key = keys[0]
    input_file = x
    output_file = y

    with open(input_file, 'rb') as f:
        data = f.read()

    f2 = Fernet(key)
    encrypted = f2.encrypt(data)

    with open(output_file, 'wb') as f:
        f.write(encrypted)

key_reader.py:

def read_keys():
    from typing import List
    f1 = open('keys.key', 'rb')
    f2: List[bytes] = []
    data: bytes
    for data in f1:
        f2.append(data)
    print(f2)
    return f2

main_file.py:

from cryptograph import file_encryptor as fe
from cryptograph import file_decryptor as fd
z1r = open('files.txt', 'r')
z2 = []
for data in z1r:
    data = data.rstrip('\n')
    z2.append(data)
fe.encrypt_file(z2[0], z2[1])
fd.decrypt_file(z2[1], z2[2])

I have just completed the premium category. The crytograph is the problem. Everything is OK, except the main_file which is causing problems.

标签: pythonpython-3.xlistfile

解决方案


错误的确切原因是

"File "F:\PyCharm Python Works\OpenCity\cryptograph\key_reader.py", line 3, in 
read_keys
f1 = open('keys.key', 'rb')"

它归咎于没有keys.key文件。因此,您应该确认“F:\PyCharm Python Works\OpenCity\cryptograph\”目录中存在“keys.key”文件。


推荐阅读