由于实习项目的需要,最近在公司完成读取绝对路径下文件夹的子目录文件。包括子文件夹,各种文档。因为项目比较急,所以用Python开发,为了方便使用,并用Wxpython做成交互界面,下面就是具体代码。
# -*- coding: utf-8 -*-
import wx
import os
def load1(event):
dialog1 = wx.DirDialog(None,'Notepad',style = wx.FD_OPEN)
if dialog1.ShowModal() == wx.ID_OK:
dirname1.SetValue(dialog1.GetPath())
#dir = open(dialog.GetPath())
#dirPath = open(dirname.GetValue().strip())
full_path = dirname1.GetValue() + '.txt'
f = open(full_path,'w')
print dirname1.GetValue().strip()
for dirpath, dirnames, filenames in os.walk(dirname1.GetValue().strip()):
print filenames
for filename in dirnames:
print >> f,(os.path.join(filename))
for filename in filenames:
print >> f,(os.path.join(filename))
app = wx.App()
win = wx.Frame(None, title="Simple Editor", size=(420,100))
loadButton1 = wx.Button(win, label='Open',pos=(225,5),size =(80,25))
loadButton1.Bind(wx.EVT_BUTTON, load1)
dirname1 = wx.TextCtrl(win,pos=(5,5),size =(210,25))
Label = wx.StaticText(win,-1, u"注:保存路径在E盘", pos = (15,30))
win.Show()
app.MainLoop()
小白第一次发博客,希望大家多多指教,共同进步。