У меня есть множество файлов баз данных в папке. В основном это выглядит так:
Для базы данных ABC у меня есть следующие файлы:
ABC.ase
ABC.dde
ABC.lpk
ABC.koi
ABCIPS.lip
Для базы данных JDE у меня есть следующие файлы:
JDE.ase
JDE.dde
JDE.lpk
JDE.koi
JDESFF.lip
Мне нужно, чтобы некоторые из этих БД были заархивированы в один файл для каждого БД, то есть:
ABC.zip
содержит все его файлы, кроме IPS, поэтому:
ABC.ase
ABC.dde
ABC.lpk
ABC.koi
Вот мой код:
import tkinter as tki
import zipfile
import os
from tkinter import messagebox
class App(object):
def __init__(self):
self.root = tki.Tk()
self.root.title('Database Zipper')
# create a Frame for the Text and Scrollbar
txt_frm = tki.Frame(self.root, width=300, height=600)
txt_frm.pack(fill="both", expand=True)
# ensure a consistent GUI size
txt_frm.grid_propagate(False)
# implement stretchability
txt_frm.grid_rowconfigure(0, weight=1)
txt_frm.grid_columnconfigure(0, weight=1)
# create a Text widget
self.txt = tki.Text(txt_frm, borderwidth=3, relief="sunken")
self.txt.config(font=("consolas", 12), undo=True, wrap='word')
self.txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)
# create a Scrollbar and associate it with txt
scrollb = tki.Scrollbar(txt_frm, command=self.txt.yview)
scrollb.grid(row=0, column=1, sticky='nsew')
self.txt['yscrollcommand'] = scrollb.set
tki.Button(txt_frm, text='ZIP', command=self.retrieve_input).grid(row=1, column=0)
def retrieve_input(self):
#zipping + filtering files
cel = c:\DBS
cip = c:\ZIP
cippath = os.listdir(cip)
lines = self.txt.get("1.0", tki.END).splitlines()
filestozip = []
for file in lines:
basename = os.path.splitext(file)[0]
filestozip.append(basename)
for basename in filestozip:
os.chdir(cip)
with zipfile.ZipFile(cel + basename + '.zip', 'w', compression=zipfile.ZIP_DEFLATED) as myzip:
for file in cippath:
if 'IPS' in file:
continue
if any(x in file for x in filestozip):
myzip.write(file)
messagebox.showinfo("Success", "Files have been zipped successfully")
app = App()
app.root.mainloop()
Когда я набираю только одну строку в текстовом поле, она работает просто отлично, однако, когда там более одной записи, например:
ABC
JDE
Я получаю два zip-файла ABC.zip
и JDE.zip
с файлами ABC и JDE внутри каждого. Как я мог предотвратить это?
Заранее спасибо!