0

I have an issue with FileNotFoundError: [Errno 2] No such file or directory: and i think it have to do with my visual studio code as on other PC it works. In debug console it prints the acctual name of the file Maybe you have any clues what could it be?

questionPass = [{'type': 'password','message': 'Enter your password','name': 'password'}]  

questionFile = [{'type': 'input','message': 'Please provide file name','name': 'io_file'}]

questionED = [ {'type': 'list','name': 'EncryptionDecryption','message': 'Please select to encrypt or decrypt the chosen file','choices': ['encrypt', 'decrypt']}]

password_provided = prompt(questionPass)
password = json.dumps(password_provided).encode('UTF-8') # Convert to type bytes
password_decoded = password.decode()
password = json.loads(password_decoded)['password'].encode()

salt = b'salt_' 
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend())

key = base64.urlsafe_b64encode(kdf.derive(password))

iof = prompt(questionFile)
io_file1 = json.dumps(iof)
io_file = json.loads(io_file1)['io_file']

ch = prompt(questionED)
cho = json.dumps(ch)
choice = json.loads(cho)['EncryptionDecryption']

#encryption part
if choice=='encrypt':        

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

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

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

    print ("\nEncryption part was done successfully")
    time.sleep(3)

 #decryption part     
elif choice=='decrypt':
    try:

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

        fernet = Fernet(key)
        decrypted = fernet.decrypt(data)

        with open(io_file, 'wb') as f:
            f.write(decrypted)
        print ("\nDecryption part was done successfully")
        time.sleep(3)
    except:
        print("Something went wrong. Please restart program and try again!")
        time.sleep(3)
Bicas
  • 1
  • Used libraries just in case: from __future__ import print_function, unicode_literals from cryptography.fernet import Fernet import cryptography import base64 import os import time import json from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC from PyInquirer import style_from_dict, Token, prompt, print_json from examples import custom_style_2 – Bicas May 21 '20 at 20:15
  • This means that `io_file = json.loads(io_file1)['io_file']` is not returning a path that exists on your workstation. – jordanm May 21 '20 at 20:16
  • @jordanm what shall i need to do in this case? – Bicas May 21 '20 at 20:18
  • 1
    Fix the path to the file in your json or place the file or directory where it should be? – jordanm May 21 '20 at 20:20
  • Does this answer your question? [FileNotFoundError](https://stackoverflow.com/questions/17658856/filenotfounderror) – wjandrea Jul 05 '20 at 16:10

0 Answers0