如何从tree目录获取文件路径以使用wxpython读取文本文件行..?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从tree目录获取文件路径以使用wxpython读取文本文件行..?相关的知识,希望对你有一定的参考价值。

#!/usr    /bin/python
# -*- coding: utf-8 -*-
import wx
import os    

class RandomObj(object):

    def __init__(self, name):
        self.name = name
class TreeExample(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Database', size=(200, 130))
        self.tree = wx.TreeCtrl(self, size=(200, 100))
        root = self.tree.AddRoot('Database')
        self.tree.AppendItem(root, 'Class A')
        files = []
        for (dirname, dirnames, filenames) in 
            os.walk('D:/Database/Class A'):

            # print path to all subdirectories first.

            for subdirname in dirnames:
                files.append(os.path.join(dirname, subdirname))
                # print path to all filenames.
                for filename in filenames:
                    self.item = files.append(os.path.join(dirname,
                            filename))
                    self.tree.AppendItem(self.tree.GetLastChild(root),
                            filename)
        self.sel = []
        for (i, j) in enumerate(files):
            if j.endswith('.txt'):
                self.sel.append(j)
        print self.sel
        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated,
                  self.tree)
    def OnActivated(self, event):
        value = event.GetItem()
        f = open(value, 'r')
        lines = f.readlines()
        print lines[0]
        print lines[1]
        print 'Double clicked on', self.tree.GetItemText(value)

我试图传递给open()的'value'是treeitemid,它需要filepath

答案

我想你必须走回树上。 这似乎有效,尽管您可能想等待更好的答案

#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
import os

class RandomObj(object):

    def __init__(self, name):
        self.name = name

class TreeExample(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, title='Database', size=(200, 130))
        self.tree = wx.TreeCtrl(self, size=(200, 100))
        root = self.tree.AddRoot('/home/public')
        self.root = '/home/public'
        self.tree.AppendItem(root, 'Music')
        files = []
        for (dirname, dirnames, filenames) in 
            os.walk('/home/public/Music'):

            # print path to all subdirectories first.

            for subdirname in dirnames:
                files.append(os.path.join(dirname, subdirname))
                # print path to all filenames.
                for filename in filenames:
                    self.item = files.append(os.path.join(dirname,
                            filename))
                    self.tree.AppendItem(self.tree.GetLastChild(root),
                            filename)
        self.sel = []
        for (i, j) in enumerate(files):
            if j.endswith('.txt'):
                self.sel.append(j)
        #print self.sel
        self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnActivated,
                  self.tree)

    def OnActivated(self, event):
        value = self.tree.GetSelection()
        fname = []
        while self.tree.GetItemParent(value):
            fname.insert(0,"/"+self.tree.GetItemText(value))
            value = self.tree.GetItemParent(value)
        fname.insert(0,self.root)
        the_file = "".join(fname)
        f = open(the_file, 'r')
        lines = f.readlines()
        print (lines[0])
        print (lines[1])
        print ('Double clicked on', str(the_file))
if __name__ == '__main__':
    app = wx.App()
    frame = TreeExample()
    frame.Show()
    app.MainLoop()

以上是关于如何从tree目录获取文件路径以使用wxpython读取文本文件行..?的主要内容,如果未能解决你的问题,请参考以下文章

使用 Flutter web (chrome) 从系统目录获取文件路径以读取文件内容 例如:CSV 或文本文件

linux查找目录之文件名指令

tree命令:文件目录结构以树形显示

使用路径从文件内部存储目录中的 zip 获取文件

如何从路径字符串中获取最后一个文件夹?

如何从文件的完整路径中获取目录?