I have written a function in Python which goes through a specified directory and gets all its files and sub-folders recursively and filtering it to only list certain file formats. How do I display this in a treeview with an different icon for folders vs files?
recursive directory function:
def test(self):
formats = ['.jpg', '.jpeg', '.txt']
for path, subdirs, files in os.walk(r'C:/Users/jmartini/Projects/Photogrammetry'):
for file in files:
filename, extension = os.path.splitext(file)
if (extension.lower() in formats):
f = os.path.join(path, file)
print f
Concept
Entire application code:
import sys
import os
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
# formatting
self.resize(550, 400)
self.setWindowTitle("Toychest")
# widgets
self.toollist = QtGui.QTreeView()
# Tabs
# signals
# main layout
mainLayout = QtGui.QGridLayout()
mainLayout.setContentsMargins(0,0,0,0)
mainLayout.addWidget(self.toollist)
self.setLayout(mainLayout)
# self.test()
# Functions
# ------------------------------------------------------------------------------
def test(self):
formats = ['.jpg', '.jpeg', '.txt']
for path, subdirs, files in os.walk(r'C:/Users/jmartini/Projects/Photogrammetry'):
for file in files:
filename, extension = os.path.splitext(file)
if (extension.lower() in formats):
f = os.path.join(path, file)
print f
# Main
# ------------------------------------------------------------------------------
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())