大家好,我是200,一個(gè)喜歡生活的程序員~
引言-應(yīng)用背景
背景:“工作中運(yùn)營人員需要手動(dòng)操作文件夾獲取文件名稱保存成txt或其他格式用于數(shù)據(jù)分析或其他用途”
問題:“由于手動(dòng)提取文件名稱(指定類型的文件)過于繁瑣,所以需要采用自動(dòng)的形式來完成這一操作”
實(shí)現(xiàn)方式:采用pythonGUI圖形界面化
GUI庫:tkinter
本文只實(shí)現(xiàn)了現(xiàn)存問題,后續(xù)會(huì)對(duì)代碼和功能進(jìn)行補(bǔ)充,連載文哦~
有需求可以提~
前期準(zhǔn)備
安裝python3
Mac版本
mac版本默認(rèn)2.7
brew install python3
Windows版本
1.在官網(wǎng)下載對(duì)應(yīng)的版本
https://www.python.org/downloads/windows/
64位下載Windows x86-64 executable installer 版本
32位下載Windows x86 executable installer 版本
2.雙擊安裝包正常安裝即可。ps:記得勾選Add PythonX.X to PATH --可以自動(dòng)添加到環(huán)境變量里哦!
?? 踩坑時(shí)間到?。?!要是用tkinter一定要安裝python3,不要問為什么!
腳本碼上
#!/usr/local/bin/python3
import tkinter as tk
import tkinter.filedialog as tk_fld
import tkinter.messagebox as messagebox
import os
#文件格式選擇類型(根據(jù)需求可自行新增)
voice_types = [
("pcm", 0),
("wav", 1)
]
text_types = [
("text", 0),
("txt", 1)
]
doc_types = [
("doc", 0),
("docx", 1)
]
xls_types = [
("xls", 0),
("xlsx", 1)
]
image_types = [
("jpg", 0),
("png", 1)
]
#設(shè)置目錄
def set_directory(arg):
set_path = tk_fld.askdirectory()
arg.set(set_path)
#消息提示
def msg_box(arg):
messagebox.showinfo("提示窗口", arg)
#獲取文件
def get_file(choose, out, arg_type):
if arg_type.get() == '0':
msg_box("類型不允許為空!")
else:
file_name_arr = get_folder_file_name(choose, arg_type.get())
out_file(out, file_name_arr)
msg_box("寫入成功")
var_choose.set('')
var_out.set('')
# 獲取文件夾內(nèi)指定文件類型名稱
def get_folder_file_name(choose, arg):
list_arr = []
for root, dirs, files in os.walk(choose.get()):
# root 表示當(dāng)前正在訪問的文件夾路徑
# dirs 表示該文件夾下的子目錄名list
# files 表示該文件夾下的文件list
for f in files:
if f.endswith(arg):
file_name = os.path.join(root, f)
new_file_name = file_name.replace(choose.get(), '').replace('/', '') + ":" + "\n"
list_arr.append(new_file_name)
return list_arr
# 生成文本
def out_file(out, arr):
full_path = "result" + '.txt' # 也可以創(chuàng)建一個(gè).doc的word文檔
file = open(out.get() + '/' + full_path, 'w') # w 的含義為可進(jìn)行讀寫
for value in arr:
file.write(value)
file.close()
# 創(chuàng)建窗口
window = tk.Tk()
# 設(shè)置窗口標(biāo)題
window.title('文件處理小工具')
# 設(shè)置窗口大小,使用小寫x
window.geometry("1000x800")
# 創(chuàng)建子容器
container = tk.LabelFrame(window, text="獲取文件夾內(nèi)全部文件名", font=24)
container.grid(row=0, column=0, padx=5, pady=5)
label_choose = tk.Label(container, text="要選擇的文件夾", font=30, width=15)
var_choose = tk.StringVar()
# 文本屬性textvariable 可變文本,與StringVar等配合著用
label_out_path = tk.Entry(container, textvariable=var_choose, font=(30), width=60)
# 設(shè)置輸入目錄 command: 指定按鈕消息的回調(diào)函數(shù);
btn_label_path = tk.Button(container, text="設(shè)置輸入目錄", font=(30),
command=lambda: set_directory(var_choose))
# 格式排版
label_choose.grid(row=0, column=0, padx=5, pady=5)
label_out_path.grid(row=0, column=1, padx=5, pady=5)
btn_label_path.grid(row=0, column=2, padx=5, pady=5)
label_out = tk.Label(container, text="要輸出的文件夾", font=30, width=15)
var_out = tk.StringVar()
# 文本屬性textvariable 可變文本,與StringVar等配合著用
label_out_path = tk.Entry(container, textvariable=var_out, font=(30), width=60)
# 設(shè)置輸入目錄 command: 指定按鈕消息的回調(diào)函數(shù);
btn_label_out_path = tk.Button(container, text="設(shè)置輸入目錄", font=(30),
command=lambda: set_directory(var_out))
# 格式排版
label_out.grid(row=1, column=0, padx=5, pady=5)
label_out_path.grid(row=1, column=1, padx=5, pady=5)
btn_label_out_path.grid(row=1, column=2, padx=5, pady=5)
label_radio = tk.Label(container, text="請(qǐng)選擇要獲取的文件類型", font=30, width=18)
label_radio_type = tk.StringVar()
for file_type, num in voice_types:
b = tk.Radiobutton(container, text=file_type, variable=label_radio_type, value=file_type)
b.grid(row=3, column=num, sticky=tk.W)
for doc_type, num1 in doc_types:
b = tk.Radiobutton(container, text=doc_type, variable=label_radio_type, value=doc_type)
b.grid(row=4, column=num1, sticky=tk.W)
for image_type, num2 in image_types:
b = tk.Radiobutton(container, text=image_type, variable=label_radio_type, value=image_type)
b.grid(row=5, column=num2, sticky=tk.W)
for text_type, num3 in text_types:
b = tk.Radiobutton(container, text=text_type, variable=label_radio_type, value=text_type)
b.grid(row=6, column=num3, sticky=tk.W)
for xls_type, num4 in xls_types:
b = tk.Radiobutton(container, text=xls_type, variable=label_radio_type, value=xls_type)
b.grid(row=7, column=num4, sticky=tk.W)
# 格式排版
label_radio.grid(row=2, column=0, sticky=tk.W)
label_radio_type.set(0)
btn_get_file_name = tk.Button(container, text='獲取文件名稱生成指定文件', font=(
30), command=lambda: get_file(var_choose, var_out, label_radio_type))
btn_get_file_name.grid(row=8, column=2, padx=5, pady=5)
# 進(jìn)入消息循環(huán), window不斷刷新
window.mainloop()
if __name__ == "__main__":
print("run " + __file__)
展示結(jié)果



最終結(jié)果

彩蛋及預(yù)告
本文為連載篇,所以后續(xù)會(huì)繼續(xù)優(yōu)化和新增需要,下期會(huì)根據(jù)新的需求新增功能,并且下期附上如何打包python應(yīng)用程序的方法,讓大家用起來更方便~
完整代碼及后續(xù)更新地址附上:https://github.com/printf200/fileScriptTool-demo
??歡迎大家star,筆芯??~,我們下期再見嘍~
可以搜索公眾號(hào)printf200關(guān)注一下哦~