Designed a Tkinter GUI app the main goal is zipping files that matches a certain pattern. It zipps the desired files together and every action is as intended. The only problem is the first files that has been zipped getting bigger and bigger by size on disk and the actual size follows if I wont terminate the terminal it goes up to 120gb or more.(Original files are 90mb). You can think the files as batches as follows:
- AAAA.src
- AAAA.txt
- AAAA.log
- BBBB.src
- BBBB.log
- So the first zipped file would be AAAA.zip and this one becomes problematic. Here's a snippet from the code. Any help would be appreciated.
def copy_move_files(self, file_paths, action):
third_input = self.get_third_input()
for file_path in file_paths:
if action == "copy":
# .....
elif action == "move":
# .....
elif action == "just_zip":
file_paths_to_zip = []
file_paths_to_delete = []
source_with_third = os.path.join(self.source_folder, third_input)
pattern = r'\d{8}_\d{6}_\d{14}'
for root, _, files in os.walk(source_with_third):
for file in files:
file_name = os.path.basename(file)
if re.match(pattern, file_name):
file_path = os.path.join(root, file)
file_paths_to_zip.append(file_path)
file_paths_to_delete.append(file_path)
else:
file_path = os.path.join(root, file)
file_paths_to_delete.append(file_path)
for file_path in file_paths_to_zip:
try:
prefix = os.path.basename(file_path)[:30]
zip_path = os.path.join(os.path.dirname(file_path), f'{prefix}.zip')
with zipfile.ZipFile(zip_path, 'a') as zipf:
arcname = os.path.basename(file_path)
zipf.write(file_path, arcname)
self.logger.info("File %s zipped successfully", os.path.basename(file_path))
except Exception as e:
# Log the error message if an exception occurs during zipping
self.logger.error("Error zipping file %s: %s", os.path.basename(file_path), str(e))
for file_path in file_paths_to_delete:
if not file_path.endswith(".zip"):
os.remove(file_path)
self.logger.info("File %s deleted successfully", os.path.basename(file_path))
self.logger.info("Files zipped successfully on %s", self.source_folder)
def just_zip(self):
try:
self.source_folder = self.source_entry.get()
self.dest_folder = self.dest_entry.get()
third_input = self.third_entry.get()
last_copy_time = datetime.now().strftime("%Y-%m-%d \n %H:%M:%S")
self.logger.info("Files will be zipped with the Chamber Name: %s", third_input)
with open("guide.txt", "r+") as file:
lines = file.readlines()
lines[0] = self.source_folder + "\n"
lines[1] = self.dest_folder + "\n"
lines[2] = self.third_entry.get() + "\n"
lines[3] = last_copy_time + "\n"
file.seek(0)
file.writelines(lines)
file.truncate()
with open("guide.txt", "r+") as file:
file_lines = file.readlines()
if len(file_lines) > 5:
file.seek(0)
file.writelines(file_lines[:5])
file.truncate()
if not self.source_folder:
print("Please select a source.")
self.logger.warning("No Source folder selected.")
return
file_paths = [os.path.join(self.source_folder, file) for file in os.listdir(self.source_folder)]
try:
threading.Thread(target=self.copy_move_files, args=(file_paths, "just_zip")).start()
except Exception as e:
logging.error("An error occurred during thread creation for zipping files: %s", str(e))
except Exception as e:
logging.error("An error occurred during zipping files: %s", str(e))
I tried the with block with only write mode but outcome was no zipped files. Tried zipf.close(). Started to get suspicious from threads.
Thank You!
I do not know what was the problem but for the people who might have a similar bug I wrote a completely different zip function from scratch and this works fine.