Python nur bestimmte dateien in zip archiv speichern?

Hallo Leute,

es geht um folgendes. Eine Funktion (siehe unten) soll ein Zip Archiv erstellen. Dieses soll alle Dateien aus dem verzeichnis /main/content/data beinhalten. Danach wird das ZIP verschlüsselt. Das Ver-, bzw. entschlüsseln funktioniert wunderbar. Nur finde ich, wenn ich das Zip Archiv dann wieder entschlüssele plötzlich nicht die Dateien aus /main/content/data - sondern ebenfalls die Ordner main, content und data. Also die werden mit in das Archiv gepackt. Das sollen Sie aber nicht. Und ich kann meinen Fehler beim besten Willen nicht finden...

Währe nett, wenn mir mal kurz jemand helfen könnte!

Danke im Voraus!

Hier mein Code:

def createEncryptedZIP(compress = zipfile.ZIP_DEFLATED):
    global zip_path
    global zip_name
    global original_path
    
    folder ='main\\content\\data'
    zip_name = simpledialog.askstring(title="Enter Filename", prompt="Enter file name for the new filecontainer")
    zip_path=os.path.join(os.path.dirname(__file__), 'main\\content\\secdrives\\')+zip_name+".secdrive"
    original_path=zip_path
    with zipfile.ZipFile(zip_path, 'w', compress) as target:
        for (root, dirs, files) in os.walk(folder):
            for file in files:
                add = os.path.join(root, file)
                target.write(add)
    to_encrypt = open(zip_path, "rb").read()
    size = len(to_encrypt)
    key = open(keyfile, "rb").read()
    encrypted = bytes(a ^ b for (a, b) in zip(to_encrypt, key))
    with open(zip_path, "wb") as encrypted_out:
        encrypted_out.write(encrypted)
    filelist = glob.glob(os.path.join('main\content\data', "*"))
    for f in filelist:
        os.remove(f)
    print(showinfo("New filecontainer created successfully", "You created a new filecontainer:\n\n"+zip_path))
    label_local.configure(text="Local File: "+zip_path)

Datei wieder entschlüsseln:

def decrypt_direct():
    file = open(zip_path, "rb").read()
    key = open(keyfile, "rb").read()
    decrypted = bytes(a ^ b for (a, b) in zip(file, key))
    with open(zip_path, "wb") as decrypted_out:
        decrypted_out.write(decrypted)
    os.rename(original_path, original_path+".zip")
    with zipfile.ZipFile(zip_path+".zip",'r') as source:
        source.extractall(decrpytion_path)
    subprocess.call(["subst y: " +'"'+os.path.join(os.path.dirname(__file__), 'main\\content\\data')+'"'], shell=True)
    print(showinfo("Decryption Successfull", "Decryption of \n\n "+zip_path+" \n\nSuccessfull!"))


Ganz am Anfang vom File:

decrpytion_path='main\\content\\data'
Python, Python 3
Python - tkinter text eingabe in variable?

Hallo,

ich habe folgendes programmiert:

import tkinter as tk
import os


root = tk.Tk()
root.geometry("400x240")


def encrypt(filename):
    to_encrypt = open(filename, "rb").read()
    size = len(to_encrypt)
    key = os.urandom(size)
    with open(filename + ".lqk", "wb") as key_out:
        key_out.write(key)
    encrypted = bytes(a ^ b for (a, b) in zip(to_encrypt, key))
    with open(filename, "wb") as encrypted_out:
        encrypted_out.write(encrypted)  
    filename = textExample.get(1.0, tk.END+"-1c")
    encrypt(filename)




def decrypt(filename, key):
    file = open(filename, "rb").read()
    key = open(key, "rb").read()
    decrypted = bytes(a ^ b for (a, b) in zip(file, key))
    with open(filename, "wb") as decrypted_out:
        decrypted_out.write(decrypted)
    fileTest = filename + ".lqk"
    try:
        os.remove(fileTest)
    except OSError as e:
        print(e)
    else:
        print("Datei erfolgreich gelöscht!")
    filename = textExample.get(1.0, tk.END+"-1c")
    decrypt(filename, filename + ".lqk")
    
    


textExample=tk.Text(root, height=10)
textExample.pack()
btnEncrypt=tk.Button(root, height=1, width=10, text="Encrypt", 
                    command=encrypt)
btnDecrypt=tk.Button(root, height=1, width=10, text="Decrypt", 
                    command=decrypt)


btnEncrypt.pack()
btnDecrypt.pack()


root.mainloop()

Nur leider funktioniert dies nicht. In der Debugging Console bekomme ich folgende Fehlermeldung:

Bei der Encrypt Funktion:

PS C:\Users\elias\Desktop\Python>  c:; cd 'c:\Users\elias\Desktop\Python'; & 'C:\Users\elias\AppData\Local\Programs\Python\Python310\python.exe' 'c:\Users\elias\.vscode\extensions\ms-python.python-2021.12.1559732655\pythonFiles\lib\python\debugpy\launcher' '52518' '--' 'c:\Users\elias\Desktop\Python\main.py'
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\elias\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
TypeError: encrypt() missing 1 required positional argument: 'filename'


Decrypt Funktion:

Traceback (most recent call last):
  File "C:\Users\elias\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
TypeError: decrypt() missing 2 required positional arguments: 'filename' and 'key'      


Wie kann ich das Problem jetzt lösen?

Danke im Vorraus!

VG!

programmieren, Python, Python 3, Tkinter

Meistgelesene Beiträge zum Thema Python