Reputation: 127
So I'm currently working on a project where I should be able to select a folder, encrypt everything inside that folder and put it into a .zip file.
The code where I encrypt the files and put them into the .zip file is this one:
with zipfile.ZipFile(path,'w') as my_zip3:
for folderName, subfolders, filenames in os.walk(directoryname):
for filename in filenames:
print(filename)
self.encrypt(filename,key)
my_zip3.write(os.path.join(folderName, filename))
Now, the problem is that when I ONLY use the "print(filename)" part, it prints all the files correctly, but when I add the other 2 lines of code to encrypt and add the files to the zip, it just gives me this error: "FileNotFoundError: [Errno 2] No such file or directory: file" I also have other parts of the code that WORK where I do the same without the encrypting part. Here's the encrypt function:
def encrypt(self, filename, key):
f = Fernet(key)
with open(filename, "rb") as file:
file_data = file.read()
encrypted_data = f.encrypt(file_data)
with open(filename, "wb") as file:
file.write(encrypted_data)
Upvotes: 0
Views: 821
Reputation: 26
I assume that the error gets thrown in the line with open(filename, "rb") as file: within the encryption. With filename, you only get the name of the file, not the path to it. Since you're walking through a directory, you could be anywhere inside that structure, so the is not found. Try concatinating the directory path as you did with os.path.join(folderName, filename).
Upvotes: 1