import os
import shutil
import tkinter as tk
from tkinter import filedialog, messagebox
import re
from collections import defaultdict
def clean_filename(filename, redundant_words):
"""Clean filename according to specified rules (remove numbers, repeated words)."""
filename = filename.split(':', 1)[-1]
filename = os.path.splitext(filename)[0]
words = filename.replace('_', ' ').lower().split()
if all(part.isdigit() for part in words):
return ' '.join(words)
words = [re.sub(r'\d+', ' ', word).strip() for word in words]
words = ' '.join(words).split()
unique_words = [w for w in words if w not in redundant_words]
seen = set()
cleaned_words = [
word for word in unique_words
if len(word) > 2 and not (word in seen or seen.add(word))
]
cleaned_filename = ' '.join(cleaned_words)
return cleaned_filename if cleaned_filename else "misc_files"
def find_redundant_words(files):
if not files:
return []
common_words = set(files[0].replace('', ' ').lower().split())
for file in files[1:]:
common_words.intersection_update(file.replace('', ' ').lower().split())
return list(common_words)
def prepare_groups(files):
if not files:
return {}
sample_filenames = [file.split(':', 1)[-1] for file in files if ':' in file]
redundant_words = find_redundant_words(sample_filenames)
groups = defaultdict(list)
for file in files:
cleaned_name = clean_filename(file, redundant_words)
groups[cleaned_name].append(file)
return groups
def show_folder_contents(event=None):
"""Display files in the selected folder."""
selection = folder_listbox.curselection()
if not selection:
return
code
folder_name = folder_listbox.get(selection[0])
parent_prefix = parent_name_entry.get().strip()
add_prefix = prefix_var.get()
group_files = group_files_var.get()
capitalize_names = capitalize_var.get()
# Modify folder name according to options.
if add_prefix:
folder_name = f"{parent_prefix} - {folder_name}"
if capitalize_names:
folder_name = ' '.join(
[word.capitalize() if word.lower() not in {'of', 'is', 'a', 'an'} else word
for word in folder_name.split()]
)
content_listbox.delete(0, tk.END)
folder_path_full = os.path.join(folder_path, folder_name)
if os.path.exists(folder_path_full):
if group_files:
subfolders = [f for f in os.listdir(folder_path_full) if os.path.isdir(os.path.join(folder_path_full, f))]
for subfolder in subfolders:
content_listbox.insert(tk.END, f"Subfolder: {subfolder}")
subfolder_path = os.path.join(folder_path_full, subfolder)
subfolder_files = os.listdir(subfolder_path)
for file in subfolder_files:
content_listbox.insert(tk.END, f" - {file}")
else:
files = os.listdir(folder_path_full)
for file in files:
content_listbox.insert(tk.END, file)
Assistant
def load_directory():
global folder_path
folder_path = filedialog.askdirectory(title="Select the folder containing your files")
if not folder_path:
return
parent_folder_name = os.path.basename(folder_path)
parent_name_entry.delete(0, tk.END)
parent_name_entry.insert(0, parent_folder_name)
try:
files = os.listdir(folder_path)
global groups
groups = prepare_groups(files)
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
folder_listbox.delete(0, tk.END)
for folder_name in groups.keys():
if add_prefix:
folder_name = f"{parent_prefix} - {folder_name}"
if capitalize_names:
folder_name = ' '.join(
[word.capitalize() if word.lower() not in {'of', 'is', 'a', 'an'} else word
for word in folder_name.split()]
)
folder_listbox.insert(tk.END, folder_name)
def execute_sorting():
if not folder_path or not groups:
messagebox.showerror("Error", "No directory loaded or no files grouped. Please load a directory first.")
return
code
try:
parent_prefix = parent_name_entry.get().strip()
add_prefix = prefix_var.get()
group_files = group_files_var.get()
capitalize_names = capitalize_var.get()
file_number = int(file_number_entry.get()) if group_files else None
for folder_name, files in groups.items():
modified_folder_name = folder_name
if add_prefix:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
if capitalize_names:
modified_folder_name = ' '.join(
[word.capitalize() if word.lower() not in {'of', 'is', 'a', 'an'} else word
for word in modified_folder_name.split()]
)
new_folder_path = os.path.join(folder_path, modified_folder_name)
os.makedirs(new_folder_path, exist_ok=True)
if group_files and file_number:
index = 0
for idx, file in enumerate(files):
subfolder_index = idx // file_number
if idx + file_number >= len(files) and subfolder_index > 0:
subfolder_index = 0
subfolder_path = os.path.join(new_folder_path, str(subfolder_index + 1))
os.makedirs(subfolder_path, exist_ok=True)
shutil.move(os.path.join(folder_path, file), os.path.join(subfolder_path, file))
else:
for file in files:
shutil.move(os.path.join(folder_path, file), os.path.join(new_folder_path, file))
messagebox.showinfo("Success", "Files sorted and organized successfully!")
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
I am not able to display the content of the folder when folder is selected from the folder Listbox.
It looks like the issue stems from how the folder_name is being handled in the show_folder_contents function. Specifically, the folder_name retrieved from the folder_listbox is being modified again based on user options (prefix and capitalization). This double modification can result in incorrect paths, preventing the contents from being displayed correctly.
Detailed Explanation
Grouping and Displaying Folders:
prepare_groups: This function groups the files and creates folder names (groups.keys()) based on the cleaned filenames.
update_displayed_folders: When displaying these grouped folder names in the folder_listbox, it applies modifications like adding a prefix and capitalizing the folder names based on user selections.
Issue in show_folder_contents:
When a folder is selected from the folder_listbox, the show_folder_contents function retrieves the already modified folder name.
The function then applies the same modifications again (prefix and capitalization), leading to an incorrect folder path (folder_path_full).
As a result, the program looks for a folder that doesn't exist, and hence, fails to display its contents.
Solution
To fix this, you should avoid modifying the folder_name again in the show_folder_contents function since it has already been modified when populating the folder_listbox. Here's how you can adjust the code:
Modify show_folder_contents to Use the Displayed Folder Name Directly:
Remove the part where you re-apply the prefix and capitalization in the show_folder_contents function. Instead, use the folder_name directly as it appears in the folder_listbox.
Ensure Consistency Between Displayed Names and Actual Folder Names:
Since update_displayed_folders adds prefixes and capitalizes names when inserting them into the folder_listbox, the show_folder_contents function should use these exact names to locate the folders.
Revised Code Snippet
Here's the updated show_folder_contents function with the necessary changes:
python
def show_folder_contents(event=None):
"""Display files in the selected folder."""
selection = folder_listbox.curselection()
if not selection:
return
folder_display_name = folder_listbox.get(selection[0])
# Remove modifications: Do not add prefix or capitalize again
# folder_name is already displayed with prefix and capitalization if selected
content_listbox.delete(0, tk.END)
folder_path_full = os.path.join(folder_path, folder_display_name)
if os.path.exists(folder_path_full):
if group_files_var.get():
subfolders = [f for f in os.listdir(folder_path_full) if os.path.isdir(os.path.join(folder_path_full, f))]
for subfolder in subfolders:
content_listbox.insert(tk.END, f"Subfolder: {subfolder}")
subfolder_path = os.path.join(folder_path_full, subfolder)
subfolder_files = os.listdir(subfolder_path)
for file in subfolder_files:
content_listbox.insert(tk.END, f" - {file}")
else:
files = os.listdir(folder_path_full)
for file in files:
content_listbox.insert(tk.END, file)
else:
messagebox.showerror("Error", f"The folder '{folder_display_name}' does not exist.")
Additional Recommendations
Mapping Display Names to Original Group Names:
To ensure accuracy, especially if folder names might have overlapping modifications, consider maintaining a separate mapping between the displayed folder names and their original group keys. This can help accurately retrieve and display folder contents without relying solely on the modified names.
python
display_to_group_map = {}
def update_displayed_folders():
parent_prefix = parent_name_entry.get().strip()
add_prefix = prefix_var.get()
capitalize_names = capitalize_var.get()
folder_listbox.delete(0, tk.END)
display_to_group_map.clear()
for folder_name in groups.keys():
modified_folder_name = folder_name
if add_prefix:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
if capitalize_names:
modified_folder_name = ' '.join(
[word.capitalize() if word.lower() not in {'of', 'is', 'a', 'an'} else word
for word in modified_folder_name.split()]
)
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = folder_name
Then, in show_folder_contents, use the original group name:
python
Complete Revised Script
Below is the complete revised script with the necessary changes incorporated:
python
Key Changes:
Avoid Double Modification:
Removed the re-application of prefix and capitalization in the show_folder_contents function.
Mapping Display Names:
Introduced a display_to_group_map to maintain a relationship between displayed folder names and their original group keys.
Reload After Sorting:
After executing sorting, the load_directory function is called again to refresh the folder_listbox with the newly organized folders.
Error Handling:
Added an error message if the selected folder does not exist.
Global Variables Initialization:
Initialized folder_path, groups, and display_to_group_map at the beginning to avoid potential UnboundLocalError.
Conclusion
By ensuring that folder names aren't modified multiple times and maintaining a clear mapping between displayed names and actual folder groupings, the application should now correctly display the contents of the selected folders. Implementing these changes should resolve the issue you encountered. If you continue to experience problems, consider adding more debug statements or logs to trace the application's behavior step-by-step.
def show_folder_contents(event=None):
"""Display files in the selected folder."""
selection = folder_listbox.curselection()
if not selection:
return
folder_display_name = folder_listbox.get(selection[0])
original_group_name = display_to_group_map.get(folder_display_name, folder_display_name)
content_listbox.delete(0, tk.END)
folder_path_full = os.path.join(folder_path, folder_display_name)
if os.path.exists(folder_path_full):
if group_files_var.get():
subfolders = [f for f in os.listdir(folder_path_full) if os.path.isdir(os.path.join(folder_path_full, f))]
for subfolder in subfolders:
content_listbox.insert(tk.END, f"Subfolder: {subfolder}")
subfolder_path = os.path.join(folder_path_full, subfolder)
subfolder_files = os.listdir(subfolder_path)
for file in subfolder_files:
content_listbox.insert(tk.END, f" - {file}")
else:
files = os.listdir(folder_path_full)
for file in files:
content_listbox.insert(tk.END, file)
else:
messagebox.showerror("Error", f"The folder '{folder_display_name}' does not exist.")
Initialize Global Variables:
Ensure that folder_path and groups are initialized properly to avoid UnboundLocalError. You can initialize them at the beginning of your script:
python
folder_path = ""
groups = {}
Handle Edge Cases Gracefully:
Add error handling to manage scenarios where the folder might not exist or other unexpected issues occur.
import os
import shutil
import tkinter as tk
from tkinter import filedialog, messagebox
import re
from collections import defaultdict
# Initialize global variables
folder_path = ""
groups = {}
display_to_group_map = {}
def clean_filename(filename, redundant_words):
"""Clean filename according to specified rules (remove numbers, repeated words)."""
filename = filename.split(':', 1)[-1]
filename = os.path.splitext(filename)[0]
words = filename.replace('_', ' ').lower().split()
if all(part.isdigit() for part in words):
return ' '.join(words)
words = [re.sub(r'\d+', ' ', word).strip() for word in words]
words = ' '.join(words).split()
unique_words = [w for w in words if w not in redundant_words]
seen = set()
cleaned_words = [
word for word in unique_words
if len(word) > 2 and not (word in seen or seen.add(word))
]
cleaned_filename = ' '.join(cleaned_words)
return cleaned_filename if cleaned_filename else "misc_files"
def find_redundant_words(files):
if not files:
return []
common_words = set(files[0].replace('_', ' ').lower().split())
for file in files[1:]:
common_words.intersection_update(file.replace('_', ' ').lower().split())
return list(common_words)
def prepare_groups(files):
if not files:
return {}
sample_filenames = [file.split(':', 1)[-1] for file in files if ':' in file]
redundant_words = find_redundant_words(sample_filenames)
groups = defaultdict(list)
for file in files:
cleaned_name = clean_filename(file, redundant_words)
groups[cleaned_name].append(file)
return groups
def show_folder_contents(event=None):
"""Display files in the selected folder."""
selection = folder_listbox.curselection()
if not selection:
return
folder_display_name = folder_listbox.get(selection[0])
# If using display_to_group_map
original_group_name = display_to_group_map.get(folder_display_name, folder_display_name)
# Clear previous contents
content_listbox.delete(0, tk.END)
folder_path_full = os.path.join(folder_path, folder_display_name)
if os.path.exists(folder_path_full):
if group_files_var.get():
subfolders = [f for f in os.listdir(folder_path_full) if os.path.isdir(os.path.join(folder_path_full, f))]
for subfolder in subfolders:
content_listbox.insert(tk.END, f"Subfolder: {subfolder}")
subfolder_path = os.path.join(folder_path_full, subfolder)
subfolder_files = os.listdir(subfolder_path)
for file in subfolder_files:
content_listbox.insert(tk.END, f" - {file}")
else:
files = os.listdir(folder_path_full)
for file in files:
content_listbox.insert(tk.END, file)
else:
messagebox.showerror("Error", f"The folder '{folder_display_name}' does not exist.")
def load_directory():
global folder_path, groups
folder_path = filedialog.askdirectory(title="Select the folder containing your files")
if not folder_path:
return
parent_folder_name = os.path.basename(folder_path)
parent_name_entry.delete(0, tk.END)
parent_name_entry.insert(0, parent_folder_name)
try:
files = os.listdir(folder_path)
groups = prepare_groups(files)
update_displayed_folders()
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
def apply_options():
update_displayed_folders()
show_folder_contents()
def update_displayed_folders():
global display_to_group_map
parent_prefix = parent_name_entry.get().strip()
add_prefix = prefix_var.get()
capitalize_names = capitalize_var.get()
folder_listbox.delete(0, tk.END)
display_to_group_map.clear()
for folder_name in groups.keys():
modified_folder_name = folder_name
if add_prefix:
modified_folder_name = f"{parent_prefix} - {modified_folder_name}"
if capitalize_names:
modified_folder_name = ' '.join(
[word.capitalize() if word.lower() not in {'of', 'is', 'a', 'an'} else word
for word in modified_folder_name.split()]
)
folder_listbox.insert(tk.END, modified_folder_name)
display_to_group_map[modified_folder_name] = folder_name
def execute_sorting():
if not folder_path or not groups:
messagebox.showerror("Error", "No directory loaded or no files grouped. Please load a directory first.")
return
try:
parent_prefix = parent_name_entry.get().strip()
add_prefix = prefix_var.get()
group_files = group_files_var.get()
capitalize_names = capitalize_var.get()
file_number = int(file_number_entry.get()) if group_files else None
for folder_display_name, group_name in display_to_group_map.items():
files = groups.get(group_name, [])
modified_folder_name = folder_display_name # Already includes prefix and capitalization
new_folder_path = os.path.join(folder_path, modified_folder_name)
os.makedirs(new_folder_path, exist_ok=True)
if group_files and file_number:
index = 0
for idx, file in enumerate(files):
subfolder_index = idx // file_number
# Adjust logic if needed
subfolder_path = os.path.join(new_folder_path, str(subfolder_index + 1))
os.makedirs(subfolder_path, exist_ok=True)
shutil.move(os.path.join(folder_path, file), os.path.join(subfolder_path, file))
else:
for file in files:
shutil.move(os.path.join(folder_path, file), os.path.join(new_folder_path, file))
messagebox.showinfo("Success", "Files sorted and organized successfully!")
load_directory() # Reload to reflect changes
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {str(e)}")
# GUI Setup
root = tk.Tk()
root.title("File Sorter")
frame = tk.Frame(root)
frame.pack(pady=20, padx=20)
parent_name_label = tk.Label(frame, text="Parent Folder Name:")
parent_name_label.grid(row=0, column=0, padx=10)
parent_name_entry = tk.Entry(frame, width=30)
parent_name_entry.grid(row=0, column=1, padx=10)
prefix_var = tk.IntVar()
prefix_check = tk.Checkbutton(frame, text="Add Parent Name Prefix", variable=prefix_var, command=apply_options)
prefix_check.grid(row=1, column=0, columnspan=2, pady=5)
group_files_var = tk.IntVar()
group_files_check = tk.Checkbutton(frame, text="Group by file number:", variable=group_files_var, command=apply_options)
group_files_check.grid(row=2, column=0)
file_number_entry = tk.Entry(frame, width=5)
file_number_entry.insert(0, "5") # Default for file grouping
file_number_entry.grid(row=2, column=1, sticky='w')
capitalize_var = tk.IntVar()
capitalize_check = tk.Checkbutton(frame, text="Capitalize Folder Names", variable=capitalize_var, command=apply_options)
capitalize_check.grid(row=3, column=0, columnspan=2, pady=5)
folder_listbox = tk.Listbox(frame, width=40, height=15)
folder_listbox.grid(row=4, column=0, columnspan=2, pady=5)
content_listbox = tk.Listbox(frame, width=40, height=15)
content_listbox.grid(row=4, column=2, columnspan=2, pady=5)
folder_listbox.bind("<<ListboxSelect>>", show_folder_contents)
browse_button = tk.Button(frame, text="Browse Directory", command=load_directory)
browse_button.grid(row=5, column=0, pady=10, sticky='w')
execute_button = tk.Button(frame, text="Execute", command=execute_sorting)
execute_button.grid(row=5, column=3, pady=10, sticky='e')
root.mainloop()