How to use Python to realize the function of one-click file renaming
This article focuses on "how to use Python to achieve one-click file renaming function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to use Python to achieve one-click file renaming function" bar!
Catalogue
On the script
View.py
Function display
Packing mode
Windows packaging method: pycharm is packaged as exe execution file method
Summary
Script view.py# create subcontainer container_rename = tk.LabelFrame (width=1200, height=800, text= "rename File", font=24) container_rename.grid (row=17, column=0, padx=5, pady=5) # Select rename folder label_rename = tk.Label (container_rename, text= "Select renamed folder", font=30, width=15) var_rename = tk.StringVar () # text attribute textvariable variable text Work with StringVar and others to set the input directory command with label_rename_file = tk.Entry (container_rename, textvariable=var_rename, font= (30), width=75) #: specify the callback function of the button message Btn_label_rename = tk.Button (container_rename, text= "set input directory", font= (30), command=lambda: set_directory (var_rename)) var_rename.set ('') # format typesetting label_rename.grid (row=18, column=0, padx=5, pady=5) label_rename_file.grid (row=18, column=1, padx=5, pady=5) btn_label_rename.grid (row=18, column=2, padx=5 Pady=5) # renamed folder label_rename_doc = tk.Label (container_rename, text= "renamed folder", font=30, width=15) var_rename_doc_name = tk.StringVar () # text attribute textvariable variable text Work with StringVar and others to set the input directory command with label_rename_doc_name = tk.Entry (container_rename, textvariable=var_rename_doc_name, font= (30), width=75) #: specify the callback function of the button message Btn_label_rename_doc = tk.Button (container_rename, text= "renamed folder", font= (30), command=lambda: set_doc_name (var_rename, var_rename_doc_name) var_rename_doc_name.set ('') # format typesetting label_rename_doc.grid (row=19, column=0, padx=5, pady=5) label_rename_doc_name.grid (row=19, column=1, padx=5, pady=5) btn_label_rename_doc.grid (row=19, column=2 Padx=5, pady=5) # Select rename file label_rename_filename = tk.Label (container_rename, text= "Select renamed file", font=30, width=15) var_rename_file = tk.StringVar () # text attribute textvariable variable text Work with StringVar and others to set the input directory command with label_rename_file_name = tk.Entry (container_rename, textvariable=var_rename_file, font= (30), width=75) #: specify the callback function of the button message Btn_label_rename_file = tk.Button (container_rename, text= "Settings input File", font= (30), command=lambda: set_file (var_rename_file)) var_rename_file.set ('') # format typesetting label_rename_filename.grid (row=20, column=0, padx=5, pady=5) label_rename_file_name.grid (row=20, column=1, padx=5, pady=5) btn_label_rename_file.grid (row=20, column=2, padx=5 Pady=5) # rename file label_rename_files = tk.Label (container_rename, text= "renamed file", font=30, width=15) var_rename_files_name = tk.StringVar () # text attribute textvariable variable text Work with StringVar and others to set the input directory command with label_rename_files_name = tk.Entry (container_rename, textvariable=var_rename_files_name, font= (30), width=75) #: specify the callback function of the button message Btn_label_rename_files = tk.Button (container_rename, text= "rename file", font= (30), command=lambda: set_file_name (var_rename_file, var_rename_files_name) var_rename_files_name.set (') # format typesetting label_rename_files.grid (row=21, column=0, padx=5, pady=5) label_rename_files_name.grid (row=21, column=1, padx=5, pady=5) btn_label_rename_files.grid (row=21 Column=2, padx=5, pady=5)
Method.py
# handle folder renaming method def name_handling (old, new): old_path = old.get () old_arr = old_path.split ("/") name = old_arr [- 1] file_type = name.split ('.') File_path = old_path [: old_path.index (name)] if len (file_type) > 1: new_path = str (file_path + new.get () +'.'+ file_type [1]). Encode () os.rename (old_path, new_path) else: new_path = str (file_path + new.get ()). Encode () os.renames (old_path) New_path) # folder renaming method def name_handling (old, new): old_path = old.get () old_arr = old_path.split ("/") name = old_arr [- 1] file_type = name.split ('.') File_path = old_path [: old_path.index (name)] if len (file_type) > 1: new_path = str (file_path + new.get () +'.'+ file_type [1]). Encode () os.rename (old_path, new_path) else: new_path = str (file_path + new.get ()). Encode () os.renames (old_path, new_path)
Control.py
# handle renaming folder named def set_doc_name (old, new): if old.get () = =''or new.get () = ='': msg_box ("renamed file not selected or modified folder name not filled in!") Else: name_handling (old, new) msg_box ("renamed successfully") clear (old, new) # handles renaming file def set_file_name (old, new): if old.get () =''or new.get () = ='': msg_box ("renamed file or modified folder name not selected!") Else: name_handling (old, new) msg_box ("renamed successfully") clear (old, new)
Common.py
# Multi-parameter processing def clear (* args): for arg in args: arg.set ('')
File_tk.py
# get the file path def set_file (arg): file_name = tk_fld.askopenfilename () arg.set (file_name) # set directory def set_directory (arg): set_path = tk_fld.askdirectory () arg.set (set_path) # prompt window def msg_box (arg): messagebox.showinfo ("prompt window", arg) function display
The new features of ⚠️ have been marked!
The same goes for the file. Oh, there's not too much to show here.
Packaging method: windows packaging method: pycharm packaging to exe execution file method
1. Install pyinstaller under PyCharm
Pip install Pyinstaller
two。 Type "pyinstaller-F-w * .py" under Terminal to make exe. The generated files are placed in the same directory dist.
-F (note uppercase) is that all library files are packaged into an exe,-w without a black console window.
Do not add the-F parameter to generate a pile of files, but run fast. Compressed is a little smaller than a single exe file. Add the-F parameter to generate an exe file, which runs slowly. At this point, I believe that everyone on "how to use Python to achieve one-click file renaming function" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!