Guest User

Untitled

a guest
May 2nd, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. from PySide import QtCore, QtGui
  2.  
  3. if __name__ == '__main__':
  4.     import sys
  5.     import os
  6.     app = QtGui.QApplication([])
  7.     view = QtGui.QTreeView()
  8.     model = QtGui.QStandardItemModel()
  9.    
  10.     path = '/ohufx/consulting/PreferredMedia/exampleTree2'
  11.     sub_dirs_dict = {}
  12.     for cur_root, cur_dirs, cur_files in os.walk(path):
  13.         if not model.rowCount():
  14.             # first iteration - add item to model
  15.             cur_root_item = QtGui.QStandardItem(cur_root)
  16.             model.appendRow(cur_root_item)
  17.         else:
  18.             # subsequent iterations need to look up the item
  19.             # from previous iterations through sub dirs
  20.             try:
  21.                 for item in sub_dirs_dict[os.path.dirname(cur_root)]:
  22.                     if item.data(QtCore.Qt.DisplayRole) == cur_root:
  23.                         cur_root_item = item
  24.                         # remove the item from the list
  25.                         sub_dirs_dict[os.path.dirname(cur_root)].remove(item)
  26.                         if not len(sub_dirs_dict):
  27.                             # if no keys are left delete the dictionary
  28.                             del sub_dirs_dict
  29.             except KeyError:
  30.                 # reached leaf
  31.                 pass
  32.  
  33.         #### SUB DIRS ##########################################
  34.         item_counter = 0
  35.         sub_dirs_dict[cur_root] = []
  36.         for d in cur_dirs:
  37.             abs_path = os.path.join(cur_root, d)
  38.             dir_item = QtGui.QStandardItem(abs_path)
  39.             cur_root_item.setChild(item_counter, dir_item)
  40.             sub_dirs_dict[cur_root].append(dir_item)
  41.             item_counter += 1
  42.            
  43.         ##### FILES ##########################################
  44.         for f in cur_files:
  45.             file_item = QtGui.QStandardItem(os.path.join(cur_root, f))
  46.             cur_root_item.setChild(item_counter, file_item)
  47.             item_counter += 1    
  48.    
  49.     view.setModel(model)
  50.     view.show()
  51.     view.raise_()
  52.     sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment