python - 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.
解决方案
错误的确切原因是
"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”文件。
推荐阅读
- listview - 如何在 Xamarin Forms 的可重用 ViewCell 中绑定命令?
- time - `hwclock -c` 的输出中的 `freq-offset-ppm` 和 `tick` 列是什么意思?
- javascript - 如何访问此数组中对象的这些属性?
- php - 如何将数据从计划的 cron 发送到 Laravel 控制器?
- python - ValueError:检查输入时出错:预期 input_2 的形状为 (224, 224, 3) 但得到的数组的形状为 (244, 244, 3)
- dropdown - 指向 url 的语义 ui-react 下拉链接
- ocaml - 如何创建和使用 .cma 文件?
- angular - Angular 6:无法设置未定义的属性
- python - 需要根据列表中元组的长度删除重复的元组
- javascript - 根据某些选择显示其他表单字段