使用Python創(chuàng)建一個(gè)文件夾結(jié)構(gòu)生成器
在本文中,我們將探討如何利用Python的wxPython庫(kù)來(lái)創(chuàng)建一個(gè)圖形用戶界面(GUI)應(yīng)用程序,該應(yīng)用程序允許用戶通過(guò)簡(jiǎn)單的文本輸入來(lái)創(chuàng)建復(fù)雜的文件夾結(jié)構(gòu)。這個(gè)程序?qū)ㄒ粋€(gè)文本框用于輸入文件夾結(jié)構(gòu)描述,一個(gè)按鈕來(lái)觸發(fā)結(jié)構(gòu)創(chuàng)建過(guò)程,以及一個(gè)目錄選擇器來(lái)指定目標(biāo)文件夾。
全部代碼
import wx
import os
import re
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("請(qǐng)選擇目標(biāo)文件夾", "錯(cuò)誤", wx.ICON_ERROR)
return
# 獲取輸入的文件夾結(jié)構(gòu)描述
folder_structure = self.memo.GetValue()
if not folder_structure:
wx.MessageBox("請(qǐng)輸入文件夾結(jié)構(gòu)描述", "錯(cuò)誤", 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()
path_stack = [base_path] # 初始化路徑棧
for line in lines:
# 使用正則表達(dá)式移除符號(hào)
clean_line = re.sub(r'[├└│─]+', '', line).strip()
# 跳過(guò)空行
if not clean_line:
continue
indent_level = len(line) - len(line.lstrip(' ')) # 計(jì)算縮進(jìn)級(jí)別
while len(path_stack) > indent_level + 1:
path_stack.pop() # 回退到正確的父路徑
if clean_line.endswith('/'):
# 創(chuàng)建文件夾
folder_name = clean_line.rstrip('/')
new_folder_path = os.path.join(path_stack[-1], folder_name)
if not os.path.exists(new_folder_path):
os.makedirs(new_folder_path)
path_stack.append(new_folder_path)
elif '.' in clean_line:
# 創(chuàng)建文件
file_name = clean_line
new_file_path = os.path.join(path_stack[-1], file_name)
try:
with open(new_file_path, 'w') as f:
f.write('') # 創(chuàng)建空文件
except PermissionError as e:
wx.MessageBox(f"權(quán)限錯(cuò)誤,無(wú)法創(chuàng)建文件:{new_file_path}\n{str(e)}", "錯(cuò)誤", wx.ICON_ERROR)
return
wx.MessageBox("文件夾和文件創(chuàng)建完成", "成功", wx.ICON_INFORMATION)
if __name__ == "__main__":
app = wx.App(False)
FolderStructureCreator(None, title="文件夾結(jié)構(gòu)創(chuàng)建器")
app.MainLoop()
步驟1:安裝wxPython
首先,你需要在你的Python環(huán)境中安裝wxPython庫(kù)。你可以通過(guò)pip安裝它:
pip install wxPython
步驟2:創(chuàng)建主窗口類(lèi)
接下來(lái),我們定義一個(gè)名為FolderStructureCreator的類(lèi),該類(lèi)繼承自wx.Frame。在這個(gè)類(lèi)的構(gòu)造函數(shù)中,我們?cè)O(shè)置窗口的大小和標(biāo)題,并初始化各種控件(如文本框、按鈕和目錄選擇器)。
import wx
import os
import re
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()
步驟3:實(shí)現(xiàn)創(chuàng)建功能
在on_create方法中,我們獲取用戶輸入的目標(biāo)文件夾路徑和文件夾結(jié)構(gòu)描述,然后調(diào)用create_structure方法來(lái)實(shí)際創(chuàng)建文件夾和文件。
def on_create(self, event):
target_folder = self.folder_picker.GetPath()
if not target_folder:
wx.MessageBox("請(qǐng)選擇目標(biāo)文件夾", "錯(cuò)誤", wx.ICON_ERROR)
return
folder_structure = self.memo.GetValue()
if not folder_structure:
wx.MessageBox("請(qǐng)輸入文件夾結(jié)構(gòu)描述", "錯(cuò)誤", wx.ICON_ERROR)
return
self.create_structure(target_folder, folder_structure)
步驟4:解析文件夾結(jié)構(gòu)描述并創(chuàng)建文件夾/文件
create_structure方法負(fù)責(zé)解析用戶輸入的文件夾結(jié)構(gòu)描述,并根據(jù)這些信息在指定的基路徑下創(chuàng)建相應(yīng)的文件夾和文件。
def create_structure(self, base_path, structure):
lines = structure.splitlines()
path_stack = [base_path]
for line in lines:
clean_line = re.sub(r'[├└│─]+', '', line).strip()
if not clean_line:
continue
indent_level = len(line) - len(line.lstrip(' '))
while len(path_stack) > indent_level + 1:
path_stack.pop()
if clean_line.endswith('/'):
folder_name = clean_line.rstrip('/')
new_folder_path = os.path.join(path_stack[-1], folder_name)
if not os.path.exists(new_folder_path):
os.makedirs(new_folder_path)
path_stack.append(new_folder_path)
elif '.' in clean_line:
file_name = clean_line
new_file_path = os.path.join(path_stack[-1], file_name)
try:
with open(new_file_path, 'w') as f:
f.write('')
except PermissionError:
wx.MessageBox("權(quán)限不足,無(wú)法創(chuàng)建文件", "錯(cuò)誤", wx.ICON_ERROR)運(yùn)行結(jié)果


以上就是如何使用Python和wxPython創(chuàng)建一個(gè)文件夾結(jié)構(gòu)生成器的完整指南。這個(gè)工具可以大大簡(jiǎn)化在文件系統(tǒng)中組織和管理文件的過(guò)程,特別是對(duì)于需要快速建立復(fù)雜文件夾結(jié)構(gòu)的開(kāi)發(fā)人員來(lái)說(shuō)非常有用。
到此這篇關(guān)于使用Python創(chuàng)建一個(gè)文件夾結(jié)構(gòu)生成器的文章就介紹到這了,更多相關(guān)Python創(chuàng)建文件夾結(jié)構(gòu)生成器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 使用raw socket進(jìn)行TCP SYN掃描實(shí)例
這篇文章主要介紹了python 使用raw socket進(jìn)行TCP SYN掃描實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python3解釋器知識(shí)點(diǎn)總結(jié)
在本篇內(nèi)容中小編給大家總結(jié)了關(guān)于Python3解釋器的用法以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。2019-02-02
淺談Matplotlib簡(jiǎn)介和pyplot的簡(jiǎn)單使用——文本標(biāo)注和箭頭
這篇文章主要介紹了淺談Matplotlib簡(jiǎn)介和pyplot的簡(jiǎn)單使用——文本標(biāo)注和箭頭,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
python使用openpyxl庫(kù)讀寫(xiě)Excel表格的方法(增刪改查操作)
這篇文章主要介紹了python使用openpyxl庫(kù)讀寫(xiě)Excel表格的方法(增刪改查操作),本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
Python如何批量處理經(jīng)緯度數(shù)據(jù)并生成位置信息
這篇文章主要介紹了Python如何批量處理經(jīng)緯度數(shù)據(jù)并生成位置信息問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
解決Python中回文數(shù)和質(zhì)數(shù)的問(wèn)題
今天小編就為大家分享一篇解決Python中回文數(shù)和質(zhì)數(shù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
終端能到import模塊 解決jupyter notebook無(wú)法導(dǎo)入的問(wèn)題
這篇文章主要介紹了在終端能到import模塊 而在jupyter notebook無(wú)法導(dǎo)入的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03

