使用Python創(chuàng)建一個文件夾結(jié)構(gòu)生成器
在本文中,我們將探討如何利用Python的wxPython庫來創(chuàng)建一個圖形用戶界面(GUI)應(yīng)用程序,該應(yīng)用程序允許用戶通過簡單的文本輸入來創(chuàng)建復(fù)雜的文件夾結(jié)構(gòu)。這個程序?qū)ㄒ粋€文本框用于輸入文件夾結(jié)構(gòu)描述,一個按鈕來觸發(fā)結(jié)構(gòu)創(chuàng)建過程,以及一個目錄選擇器來指定目標文件夾。
全部代碼
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): # 獲取目標文件夾路徑 target_folder = self.folder_picker.GetPath() if not target_folder: wx.MessageBox("請選擇目標文件夾", "錯誤", 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() 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('/'): # 創(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)限錯誤,無法創(chuàng)建文件:{new_file_path}\n{str(e)}", "錯誤", 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庫。你可以通過pip安裝它:
pip install wxPython
步驟2:創(chuàng)建主窗口類
接下來,我們定義一個名為FolderStructureCreator的類,該類繼承自wx.Frame。在這個類的構(gòu)造函數(shù)中,我們設(shè)置窗口的大小和標題,并初始化各種控件(如文本框、按鈕和目錄選擇器)。
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:實現(xiàn)創(chuàng)建功能
在on_create方法中,我們獲取用戶輸入的目標文件夾路徑和文件夾結(jié)構(gòu)描述,然后調(diào)用create_structure方法來實際創(chuàng)建文件夾和文件。
def on_create(self, event): target_folder = self.folder_picker.GetPath() if not target_folder: wx.MessageBox("請選擇目標文件夾", "錯誤", 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)
步驟4:解析文件夾結(jié)構(gòu)描述并創(chuàng)建文件夾/文件
create_structure方法負責解析用戶輸入的文件夾結(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)限不足,無法創(chuàng)建文件", "錯誤", wx.ICON_ERROR)
運行結(jié)果
以上就是如何使用Python和wxPython創(chuàng)建一個文件夾結(jié)構(gòu)生成器的完整指南。這個工具可以大大簡化在文件系統(tǒng)中組織和管理文件的過程,特別是對于需要快速建立復(fù)雜文件夾結(jié)構(gòu)的開發(fā)人員來說非常有用。
到此這篇關(guān)于使用Python創(chuàng)建一個文件夾結(jié)構(gòu)生成器的文章就介紹到這了,更多相關(guān)Python創(chuàng)建文件夾結(jié)構(gòu)生成器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 使用raw socket進行TCP SYN掃描實例
這篇文章主要介紹了python 使用raw socket進行TCP SYN掃描實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05淺談Matplotlib簡介和pyplot的簡單使用——文本標注和箭頭
這篇文章主要介紹了淺談Matplotlib簡介和pyplot的簡單使用——文本標注和箭頭,具有一定借鑒價值,需要的朋友可以參考下2018-01-01python使用openpyxl庫讀寫Excel表格的方法(增刪改查操作)
這篇文章主要介紹了python使用openpyxl庫讀寫Excel表格的方法(增刪改查操作),本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05Python如何批量處理經(jīng)緯度數(shù)據(jù)并生成位置信息
這篇文章主要介紹了Python如何批量處理經(jīng)緯度數(shù)據(jù)并生成位置信息問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08解決Python中回文數(shù)和質(zhì)數(shù)的問題
今天小編就為大家分享一篇解決Python中回文數(shù)和質(zhì)數(shù)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11終端能到import模塊 解決jupyter notebook無法導(dǎo)入的問題
這篇文章主要介紹了在終端能到import模塊 而在jupyter notebook無法導(dǎo)入的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03