Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PySide import QtCore, QtGui
- if __name__ == '__main__':
- import sys
- import os
- app = QtGui.QApplication([])
- view = QtGui.QTreeView()
- model = QtGui.QStandardItemModel()
- path = '/ohufx/consulting/PreferredMedia/exampleTree2'
- sub_dirs_dict = {}
- for cur_root, cur_dirs, cur_files in os.walk(path):
- if not model.rowCount():
- # first iteration - add item to model
- cur_root_item = QtGui.QStandardItem(cur_root)
- model.appendRow(cur_root_item)
- else:
- # subsequent iterations need to look up the item
- # from previous iterations through sub dirs
- try:
- for item in sub_dirs_dict[os.path.dirname(cur_root)]:
- if item.data(QtCore.Qt.DisplayRole) == cur_root:
- cur_root_item = item
- # remove the item from the list
- sub_dirs_dict[os.path.dirname(cur_root)].remove(item)
- if not len(sub_dirs_dict):
- # if no keys are left delete the dictionary
- del sub_dirs_dict
- except KeyError:
- # reached leaf
- pass
- #### SUB DIRS ##########################################
- item_counter = 0
- sub_dirs_dict[cur_root] = []
- for d in cur_dirs:
- abs_path = os.path.join(cur_root, d)
- dir_item = QtGui.QStandardItem(abs_path)
- cur_root_item.setChild(item_counter, dir_item)
- sub_dirs_dict[cur_root].append(dir_item)
- item_counter += 1
- ##### FILES ##########################################
- for f in cur_files:
- file_item = QtGui.QStandardItem(os.path.join(cur_root, f))
- cur_root_item.setChild(item_counter, file_item)
- item_counter += 1
- view.setModel(model)
- view.show()
- view.raise_()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment