使用wxPython創(chuàng)建一個文件夾結(jié)構(gòu)生成器
你是否曾經(jīng)因?yàn)樾枰謩觿?chuàng)建復(fù)雜的文件夾結(jié)構(gòu)而感到頭疼?是不是覺得一層一層地新建文件夾,尤其是帶有子文件夾和文件的文件系統(tǒng)結(jié)構(gòu),實(shí)在是太繁瑣了?如果你經(jīng)常處理項目中的文件組織,那么我為你帶來了一種簡單又高效的解決方案。
C:\pythoncode\new\chromesnapshoot.py
今天,我將通過一個有趣的項目展示如何利用 wxPython 來創(chuàng)建一個文件夾結(jié)構(gòu)生成器,幫助你自動化地創(chuàng)建文件夾和文件結(jié)構(gòu),只需輸入一個簡單的描述。讓我們一起探索如何通過代碼生成你需要的文件系統(tǒng)結(jié)構(gòu)吧!
全部代碼
import wx
import os
class FolderStructureCreator(wx.Frame):
def __init__(self, parent, title):
super().__init__(parent, title=title, size=(600, 400))
# 創(chuàng)建面板
panel = wx.Panel(self)
# 創(chuàng)建控件
self.memo = wx.TextCtrl(panel, style=wx.TE_MULTILINE, size=(500, 200), pos=(50, 50))
self.create_button = wx.Button(panel, label="創(chuàng)建", pos=(50, 270))
self.folder_picker = wx.DirPickerCtrl(panel, path="", size=(500, -1), pos=(50, 300))
# 綁定事件
self.create_button.Bind(wx.EVT_BUTTON, self.on_create)
self.Show()
def on_create(self, event):
# 獲取目標(biāo)文件夾路徑
target_folder = self.folder_picker.GetPath()
if not target_folder:
wx.MessageBox("請選擇目標(biāo)文件夾", "錯誤", wx.ICON_ERROR)
return
# 獲取輸入的文件夾結(jié)構(gòu)描述
folder_structure = self.memo.GetValue()
if not folder_structure:
wx.MessageBox("請輸入文件夾結(jié)構(gòu)描述", "錯誤", wx.ICON_ERROR)
return
# 根據(jù)文件夾結(jié)構(gòu)描述創(chuàng)建文件夾和文件
self.create_structure(target_folder, folder_structure)
def create_structure(self, base_path, structure):
lines = structure.splitlines()
current_path = base_path
for line in lines:
# 處理文件夾
if '├──' in line or '└──' in line:
folder_name = line.strip().split('──')[-1].strip()
new_folder_path = os.path.join(current_path, folder_name)
if not os.path.exists(new_folder_path):
os.makedirs(new_folder_path)
current_path = new_folder_path
# 處理文件(將后綴名改為 .txt)
elif '.' in line: # 判斷是否為文件
file_name = line.strip()
# 將文件名后綴改為 .txt
file_name = os.path.splitext(file_name)[0] + '.txt'
file_path = os.path.join(current_path, file_name)
if not os.path.exists(file_path):
with open(file_path, 'w') as f:
f.write('') # 創(chuàng)建空的 .txt 文件
# 返回上一層文件夾
if line.strip().startswith('└──') or line.strip().startswith('├──'):
current_path = os.path.dirname(current_path)
wx.MessageBox("文件夾和文件創(chuàng)建完成", "成功", wx.ICON_INFORMATION)
if __name__ == "__main__":
app = wx.App(False)
FolderStructureCreator(None, title="文件夾結(jié)構(gòu)創(chuàng)建器")
app.MainLoop()項目目標(biāo)
我們將創(chuàng)建一個圖形用戶界面(GUI)應(yīng)用,用戶可以:
輸入一個描述文件夾結(jié)構(gòu)的文本,例如類似于樹狀圖的格式。
選擇目標(biāo)文件夾(即將創(chuàng)建文件夾和文件的地方)。
點(diǎn)擊按鈕后,程序自動根據(jù)描述創(chuàng)建相應(yīng)的文件夾和文件。
開始之前的準(zhǔn)備
要實(shí)現(xiàn)這個項目,我們需要使用以下工具:
wxPython:用于創(chuàng)建桌面 GUI,簡單而強(qiáng)大,非常適合我們這個文件管理工具。
os:Python 的標(biāo)準(zhǔn)庫,提供文件和文件夾操作接口。
項目實(shí)現(xiàn)
接下來,我們逐行講解這個項目的代碼,并一步步讓你了解它的工作原理。
1. 導(dǎo)入必需的庫
import wx import os
wx 是我們用來創(chuàng)建圖形界面的庫。
os 是標(biāo)準(zhǔn)庫,用來處理文件和文件夾的創(chuàng)建、刪除等操作。
2. 創(chuàng)建主應(yīng)用窗口
我們繼承 wx.Frame 來創(chuàng)建一個窗口,這就是我們應(yīng)用的主界面。
class FolderStructureCreator(wx.Frame):
def __init__(self, parent, title):
super().__init__(parent, title=title, size=(600, 400))
# 創(chuàng)建面板
panel = wx.Panel(self)
# 創(chuàng)建控件
self.memo = wx.TextCtrl(panel, style=wx.TE_MULTILINE, size=(500, 200), pos=(50, 50))
self.create_button = wx.Button(panel, label="創(chuàng)建", pos=(50, 270))
self.folder_picker = wx.DirPickerCtrl(panel, path="", size=(500, -1), pos=(50, 300))
# 綁定事件
self.create_button.Bind(wx.EVT_BUTTON, self.on_create)
self.Show()
wx.Frame 是 wxPython 提供的基礎(chǔ)窗口類。我們通過它來創(chuàng)建一個窗口并添加控件。
控件:
self.memo:一個多行文本框,用戶可以在這里輸入文件夾結(jié)構(gòu)描述。
self.create_button:一個按鈕,當(dāng)用戶點(diǎn)擊時,程序?qū)⒏鶕?jù)描述創(chuàng)建文件夾和文件。
self.folder_picker:一個文件夾選擇控件,用戶用它來選擇目標(biāo)文件夾。
3. 處理按鈕點(diǎn)擊事件
當(dāng)用戶點(diǎn)擊“創(chuàng)建”按鈕時,我們會讀取文本框中的內(nèi)容,并根據(jù)用戶指定的路徑創(chuàng)建文件夾和文件。
def on_create(self, event):
# 獲取目標(biāo)文件夾路徑
target_folder = self.folder_picker.GetPath()
if not target_folder:
wx.MessageBox("請選擇目標(biāo)文件夾", "錯誤", wx.ICON_ERROR)
return
# 獲取輸入的文件夾結(jié)構(gòu)描述
folder_structure = self.memo.GetValue()
if not folder_structure:
wx.MessageBox("請輸入文件夾結(jié)構(gòu)描述", "錯誤", wx.ICON_ERROR)
return
# 根據(jù)文件夾結(jié)構(gòu)描述創(chuàng)建文件夾和文件
self.create_structure(target_folder, folder_structure)
我們首先檢查用戶是否選擇了目標(biāo)文件夾,若沒有,彈出提示框。
然后,獲取文本框中的文件夾結(jié)構(gòu)描述,若為空,也彈出錯誤提示。
如果一切正常,調(diào)用 self.create_structure() 函數(shù)來處理文件夾結(jié)構(gòu)的創(chuàng)建。
4. 解析文件夾結(jié)構(gòu)并創(chuàng)建文件夾和文件
接下來,我們的 create_structure 方法負(fù)責(zé)解析用戶輸入的文件夾結(jié)構(gòu)并實(shí)際創(chuàng)建文件夾和文件。
def create_structure(self, base_path, structure):
lines = structure.splitlines()
current_path = base_path
for line in lines:
# 處理文件夾
if '├──' in line or '└──' in line:
folder_name = line.strip().split('──')[-1].strip()
new_folder_path = os.path.join(current_path, folder_name)
if not os.path.exists(new_folder_path):
os.makedirs(new_folder_path)
current_path = new_folder_path
# 處理文件
elif '.' in line:
file_name = line.strip()
file_path = os.path.join(current_path, file_name)
if not os.path.exists(file_path):
with open(file_path, 'w') as f:
f.write('') # 創(chuàng)建空文件
# 返回上一層文件夾
if line.strip().startswith('└──') or line.strip().startswith('├──'):
current_path = os.path.dirname(current_path)
wx.MessageBox("文件夾和文件創(chuàng)建完成", "成功", wx.ICON_INFORMATION)
分解結(jié)構(gòu):我們首先將輸入的文件夾結(jié)構(gòu)文本按行分割,每一行代表一個文件夾或文件。
創(chuàng)建文件夾:當(dāng)檢測到 ├── 或 └──(樹狀結(jié)構(gòu)符號),我們認(rèn)為這一行是一個文件夾,接著就創(chuàng)建這個文件夾。
創(chuàng)建文件:當(dāng)行中包含文件擴(kuò)展名(例如 .txt),我們認(rèn)為這是一個文件,接著在當(dāng)前路徑下創(chuàng)建這個文件。
回退路徑:通過檢查行中的樹狀符號,程序會自動返回到上一級目錄,確保目錄結(jié)構(gòu)正確。
5. 完整的代碼展示
import wx
import os
class FolderStructureCreator(wx.Frame):
def __init__(self, parent, title):
super().__init__(parent, title=title, size=(600, 400))
# 創(chuàng)建面板
panel = wx.Panel(self)
# 創(chuàng)建控件
self.memo = wx.TextCtrl(panel, style=wx.TE_MULTILINE, size=(500, 200), pos=(50, 50))
self.create_button = wx.Button(panel, label="創(chuàng)建", pos=(50, 270))
self.folder_picker = wx.DirPickerCtrl(panel, path="", size=(500, -1), pos=(50, 300))
# 綁定事件
self.create_button.Bind(wx.EVT_BUTTON, self.on_create)
self.Show()
def on_create(self, event):
target_folder = self.folder_picker.GetPath()
if not target_folder:
wx.MessageBox("請選擇目標(biāo)文件夾", "錯誤", wx.ICON_ERROR)
return
folder_structure = self.memo.GetValue()
if not folder_structure:
wx.MessageBox("請輸入文件夾結(jié)構(gòu)描述", "錯誤", wx.ICON_ERROR)
return
self.create_structure(target_folder, folder_structure)
def create_structure(self, base_path, structure):
lines = structure.splitlines()
current_path = base_path
for line in lines:
if '├──' in line or '└──' in line:
folder_name = line.strip().split('──')[-1].strip()
new_folder_path = os.path.join(current_path, folder_name)
if not os.path.exists(new_folder_path):
os.makedirs(new_folder_path)
current_path = new_folder_path
elif '.' in line:
file_name = line.strip()
file_path = os.path.join(current_path, file_name)
if not os.path.exists(file_path):
with open(file_path, 'w') as f:
f.write('')
if line.strip().startswith('└──') or line.strip().startswith('├──'):
current_path = os.path.dirname(current_path)
wx.MessageBox("文件夾和文件創(chuàng)建完成", "成功", wx.ICON_INFORMATION)
if __name__ == "__main__":
app = wx.App(False)
FolderStructureCreator(None, title="文件夾結(jié)構(gòu)創(chuàng)建器")
app.MainLoop()
運(yùn)行結(jié)果


總結(jié)
通過這個小項目,我們學(xué)到了如何結(jié)合 wxPython 和 os 庫來創(chuàng)建一個強(qiáng)大的文件夾結(jié)構(gòu)生成器。只需簡單的文本輸入和點(diǎn)擊按鈕,我們就能自動化地生成復(fù)雜的文件夾和文件結(jié)構(gòu)。你可以在日常工作中,尤其是項目管理和文檔管理中,使用這個工具來快速創(chuàng)建文件系統(tǒng)結(jié)構(gòu),節(jié)省時間和精力。
到此這篇關(guān)于使用wxPython創(chuàng)建一個文件夾結(jié)構(gòu)生成器的文章就介紹到這了,更多相關(guān)wxPython創(chuàng)建文件夾結(jié)構(gòu)生成器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Python中pandas.DataFrame重置索引名稱的實(shí)例
今天小編就為大家分享一篇在Python中pandas.DataFrame重置索引名稱的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
python中?conda?虛擬環(huán)境管理和jupyter內(nèi)核管理
這篇文章主要介紹了python中?conda?虛擬環(huán)境管理和jupyter內(nèi)核管理,文章基于pyhton以及conda的虛擬環(huán)境創(chuàng)建、刪除、jupyter添加、刪除虛擬kernel的方法,需要的朋友可以參考一下2022-04-04
python讀寫json文件的簡單實(shí)現(xiàn)
這篇文章主要介紹了python讀寫json文件的簡單實(shí)現(xiàn),實(shí)例分析了各種讀寫json的方法和技巧,有興趣的可以了解一下2017-04-04
Python實(shí)現(xiàn)計算兩個指定日期相差幾年幾月幾日
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)計算兩個日期之間相差多少年,多少月,多少天,文中的的示例代碼講解詳細(xì),需要的可以參考下2024-02-02
Python將Excel表格按某列拆分為多個sheet實(shí)現(xiàn)過程
這篇文章主要為大家介紹了Python實(shí)現(xiàn)將Excel表格按某列拆分為多個sheet,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05

