1、价值上万元的专业的PPT报告模板。
2、专业案例分析和解读笔记。
3、实用的Excel、Word、PPT技巧。
4、VIP讨论群,共享资源。
5、优惠的会员商品。
6、一次付费只需129元,即可下载本站文章涉及的文件和软件。
你是否曾经因为需要手动创建复杂的文件夹结构而感到头疼?是不是觉得一层一层地新建文件夹,尤其是带有子文件夹和文件的文件系统结构,实在是太繁琐了?如果你经常处理项目中的文件组织,那么我为你带来了一种简单又高效的解决方案。
C:pythoncodenewchromesnapshoot.py
今天,我将通过一个有趣的项目展示如何利用 wxPython 来创建一个文件夹结构生成器,帮助你自动化地创建文件夹和文件结构,只需输入一个简单的描述。让我们一起探索如何通过代码生成你需要的文件系统结构吧!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
import wx import os class FolderStructureCreator(wx.Frame): def __init__( self , parent, title): super ().__init__(parent, title = title, size = ( 600 , 400 )) # 创建面板 panel = wx.Panel( self ) # 创建控件 self .memo = wx.TextCtrl(panel, style = wx.TE_MULTILINE, size = ( 500 , 200 ), pos = ( 50 , 50 )) self .create_button = wx.Button(panel, label = "创建" , 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 # 获取输入的文件夹结构描述 folder_structure = self .memo.GetValue() if not folder_structure: wx.MessageBox( "请输入文件夹结构描述" , "错误" , 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 # 处理文件(将后缀名改为 .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('') # 创建空的 .txt 文件 # 返回上一层文件夹 if line.strip().startswith( '└──' ) or line.strip().startswith( '├──' ): current_path = os.path.dirname(current_path) wx.MessageBox( "文件夹和文件创建完成" , "成功" , wx.ICON_INFORMATION) if __name__ = = "__main__" : app = wx.App( False ) FolderStructureCreator( None , title = "文件夹结构创建器" ) app.MainLoop() |
我们将创建一个图形用户界面(GUI)应用,用户可以:
输入一个描述文件夹结构的文本,例如类似于树状图的格式。
选择目标文件夹(即将创建文件夹和文件的地方)。
点击按钮后,程序自动根据描述创建相应的文件夹和文件。
要实现这个项目,我们需要使用以下工具:
wxPython:用于创建桌面 GUI,简单而强大,非常适合我们这个文件管理工具。
os:Python 的标准库,提供文件和文件夹操作接口。
接下来,我们逐行讲解这个项目的代码,并一步步让你了解它的工作原理。
1
2
|
import wx import os |
wx 是我们用来创建图形界面的库。
os 是标准库,用来处理文件和文件夹的创建、删除等操作。
我们继承 wx.Frame 来创建一个窗口,这就是我们应用的主界面。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class FolderStructureCreator(wx.Frame): def __init__( self , parent, title): super ().__init__(parent, title = title, size = ( 600 , 400 )) # 创建面板 panel = wx.Panel( self ) # 创建控件 self .memo = wx.TextCtrl(panel, style = wx.TE_MULTILINE, size = ( 500 , 200 ), pos = ( 50 , 50 )) self .create_button = wx.Button(panel, label = "创建" , 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 提供的基础窗口类。我们通过它来创建一个窗口并添加控件。
控件:
self.memo:一个多行文本框,用户可以在这里输入文件夹结构描述。
self.create_button:一个按钮,当用户点击时,程序将根据描述创建文件夹和文件。
self.folder_picker:一个文件夹选择控件,用户用它来选择目标文件夹。
当用户点击“创建”按钮时,我们会读取文本框中的内容,并根据用户指定的路径创建文件夹和文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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( "请输入文件夹结构描述" , "错误" , wx.ICON_ERROR) return # 根据文件夹结构描述创建文件夹和文件 self .create_structure(target_folder, folder_structure) |
我们首先检查用户是否选择了目标文件夹,若没有,弹出提示框。
然后,获取文本框中的文件夹结构描述,若为空,也弹出错误提示。
如果一切正常,调用 self.create_structure() 函数来处理文件夹结构的创建。
接下来,我们的 create_structure 方法负责解析用户输入的文件夹结构并实际创建文件夹和文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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( "文件夹和文件创建完成" , "成功" , wx.ICON_INFORMATION) |
分解结构:我们首先将输入的文件夹结构文本按行分割,每一行代表一个文件夹或文件。
创建文件夹:当检测到 ├── 或 └──(树状结构符号),我们认为这一行是一个文件夹,接着就创建这个文件夹。
创建文件:当行中包含文件扩展名(例如 .txt),我们认为这是一个文件,接着在当前路径下创建这个文件。
回退路径:通过检查行中的树状符号,程序会自动返回到上一级目录,确保目录结构正确。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
import wx import os class FolderStructureCreator(wx.Frame): def __init__( self , parent, title): super ().__init__(parent, title = title, size = ( 600 , 400 )) # 创建面板 panel = wx.Panel( self ) # 创建控件 self .memo = wx.TextCtrl(panel, style = wx.TE_MULTILINE, size = ( 500 , 200 ), pos = ( 50 , 50 )) self .create_button = wx.Button(panel, label = "创建" , 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 folder_structure = self .memo.GetValue() if not folder_structure: wx.MessageBox( "请输入文件夹结构描述" , "错误" , 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( "文件夹和文件创建完成" , "成功" , wx.ICON_INFORMATION) if __name__ = = "__main__" : app = wx.App( False ) FolderStructureCreator( None , title = "文件夹结构创建器" ) app.MainLoop() |
通过这个小项目,我们学到了如何结合 wxPython 和 os 库来创建一个强大的文件夹结构生成器。只需简单的文本输入和点击按钮,我们就能自动化地生成复杂的文件夹和文件结构。你可以在日常工作中,尤其是项目管理和文档管理中,使用这个工具来快速创建文件系统结构,节省时间和精力。
学习资料见知识星球。
以上就是今天要分享的技巧,你学会了吗?若有什么问题,欢迎在下方留言。
快来试试吧,小琥 my21ke007。获取 1000个免费 Excel模板福利!
更多技巧, www.excelbook.cn
欢迎 加入 零售创新 知识星球,知识星球主要以数据分析、报告分享、数据工具讨论为主;
1、价值上万元的专业的PPT报告模板。
2、专业案例分析和解读笔记。
3、实用的Excel、Word、PPT技巧。
4、VIP讨论群,共享资源。
5、优惠的会员商品。
6、一次付费只需129元,即可下载本站文章涉及的文件和软件。