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

Meistgelesene Beiträge zum Thema Python