Untitled

mail@pastecode.io avatar
unknown
python
a year ago
2.0 kB
4
Indexable
Never
import os
import tkinter as tk
from tkinter import filedialog
import tkinter.messagebox
import datetime
import time

def upgrade_de(extension, directory):
    de = {}
    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 = directory_entry.get()
    extension = extension_entry.get()

    txt_directory='/home/nthquan/Desktop/test/txt/'
    extension = extension.lower()

    de = upgrade_de(extension, directory)

    for d in de.keys():
        for ext in de[d]:
            file_path = txt_directory +\
                f'{ext}_file_list_{int(round(time.time() * 1000))}.txt'

            with open(file_path, 'w', encoding= 'utf-8') as f:
                for root, dirs, files in os.walk(d):
                    for file in sorted(files, 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('400x150')
#root.iconbitmap('logo.ico')

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

directory_entry = tk.Entry(root, width=60)
directory_entry.insert(0, "., .")

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.insert(0, "*, txt")

extension_entry.pack()

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

root.mainloop()