Extension rename GUI

Shared by: vectorprobe

python

1
import os
2
import customtkinter as kint
3
import time
4
import random
5

6

7

8

9
# rename_files()
10

11
class app(kint.CTk):
12
    def __init__(self):
13
        super().__init__()
14

15
        self.labelling = kint.CTkLabel(self, text='File renamer')
16
        self.labelling.pack(pady=10)
17
    
18
        
19
        self.frame = kint.CTkFrame(self)
20
        self.frame.pack(padx=20,pady=8)
21

22
        self.frame_path = kint.CTkFrame(self.frame)
23
        self.frame_path.pack(padx=20,pady=20)
24

25
        self.frame_ext = kint.CTkFrame(self.frame)
26
        self.frame_ext.pack(padx=20,pady=20)
27

28
        self.label_path = kint.CTkLabel(self.frame_path, text='Example \'C:\\Users\\Username\\Music\\audiomack\'')
29
        self.label_path.pack(pady=10, padx=5)
30

31
        self.path_text = kint.CTkEntry(self.frame_path, placeholder_text='paste file path here ')
32
        self.path_text.pack(padx=10, pady=10)
33

34
        self.label_ext = kint.CTkLabel(self.frame_ext, text='Example Eg\'.exe, .mp3, .txt, .json...etc\'')
35
        self.label_ext.pack(pady=10, padx=5)
36

37

38
        self.extension = kint.CTkEntry(self.frame_ext, placeholder_text='enter file extension')
39
        self.extension.pack(padx=10, pady=10)
40

41
        self.Button = kint.CTkButton(self.frame, text="rename", command=self.clicked)
42
        self.Button.pack(side="top", padx=40, pady=10)
43
        
44

45
    def clicked(self):
46
        path = self.path_text.get()
47
        var_ext = self.extension.get()
48
        print('click of a button',path)
49
        def rename_files():
50
            # 1. Get file names from a folder
51
            file_list = os.listdir(path)
52
            print(file_list)
53
            saved_path = os.getcwd()
54
            print("Current Working Directory is "+saved_path)
55
            os.chdir(path)
56

57
            # 2. For each file, rename filename
58
            for file_name in file_list:
59
                # os.rename(file_name, file_name.translate(None, "jinx"))
60
                os.rename(file_name, f'{file_name}{var_ext}')
61

62
            os.chdir(saved_path)
63
        rename_files()
64

65

66
App = app()
67
App.mainloop()
68

69