Untitled

mail@pastecode.io avatar
unknown
python
a year ago
3.0 kB
3
Indexable
Never
# U:\PERSONAL DOCUMENT SUBMISSIONS, C:\Users\hpt9hc\Downloads\Trace, U:\SampleTask

# *,*,*,PDf,MF4,XLSX,Zip,dat,txt
# CODE:############################################################3\
import os
import tkinter as tk
from tkinter import filedialog
import tkinter.messagebox
import datetime

def upgrade_de(extension,directory):
    de = {}

    if len(extension.split(',')) == 1:
        extension = len(directory.split(',')) * (extension + ',')

    for ext, dir in zip(extension.split(','), directory.split(',')):
        ext = ext.strip()
        dir = dir.strip()
        if dir in de:
            de[dir].append(ext)
        else:
            de[dir] = [ext]
    return de
        
def list_files():
    directory = get_dir()
    extension = extension_entry.get()
    
    txt_directory='txt/'

    extension = extension.lower()
    
    de = upgrade_de(extension,directory)

    for d in de.keys():
        for ext in de[d]:
            if ext == "*":
                ext = "all" 
            foldername = os.path.basename(d)
            file_path = txt_directory + f'{foldername}_{ext}_file_list_{datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.txt'
            with open(file_path, 'w', encoding= 'utf-8') as f:
                for root, dirs, files in os.walk(d):
                    ft = sorted(files, key=lambda x: os.path.splitext(x)[0],reverse=False)
                    for file in sorted(ft, key=lambda x: os.path.splitext(x)[1],reverse=False): 
                        if ext == 'all' or file.endswith(ext):
                            file_path2 = os.path.join(root, file)
                            f.write(file_path2 + "\n")

    tkinter.messagebox.showinfo('Notification', 'Done!!!')
    # os.startfile(os.path.dirname(file_path))
 
root = tk.Tk()
root.title("File List Generator")
root.geometry()
# root.iconbitmap('logo.ico')

directory_label = tk.Label(root, text="Directory Path:")
directory_label.config(font=(30))
directory_label.pack()

directory_entry = tk.Text(root)
directory_entry.insert(tk.END, "")

directory_entry.config(width=30, height=5)
directory_entry.pack()

extension_label = tk.Label(root, text="File Extension:")
extension_label.config(font=(30))
extension_label.pack()

extension_entry = tk.Entry(root, width=30)
extension_entry.pack()

generate_button = tk.Button(root, text="Generate List", command=list_files)
generate_button.config(font=(40))
generate_button.pack()

# def on_entry_changed():
#     directory_entry.config(width=len(directory_entry.get()))

# directory_entry.config(width=10)
# expand_btn =\
#     tk.Button(
#         root, text="Fit to text",
#         command=lambda: on_entry_changed())
# expand_btn.pack()

def get_dir():
    return directory_entry.get("1.0", tk.END)

btn_fit = tk.Button(
    root, text="Fit to text",
        command=lambda: directory_entry.config(
            width=30, height=len(get_dir())/30 + get_dir().count("\n")))
btn_fit.pack()

root.mainloop()