0

I have 2 questions in trying to retrieve a set of data from a directory and displays it out into the ListWidget.

As I am a linux user, I set my ListWidget to read my directory from Desktop in which insides contains say 5 folders and 5 misc items (.txt, .py etc)

  1. Currently I am trying to make my ListWidget to display just the folders but apparently it does that but it also displays all the items, making it a total of 10 items instead of 5. I tried looking up on the net but I am unable to find any info. Can someone help me?

  2. Pertaining to Qns 1, I am wondering if it is possible to display the top 3 recent folders in the ListWidget, if a checkbox is being checked?

    import glob
    import os
    
    
    def test(object):
    testList = QListWidget()
    localDir =  os.listdir("/u/ykt/Desktop/test")
    testList.addItems(localDir)
    
dissidia
  • 1,531
  • 3
  • 23
  • 53

2 Answers2

0
  1. I guess that you are expecting that os.listdir() will return only the directory names from the given path. Actually it returns the file names too. If you want to add only directories to the listWidget, do the following:

    import os
    osp = os.path
    def test(object):
        testList = QListWidget()
        dirPath = "/u/ykt/Desktop/test"
        localDir = os.listdir(dirPath)
        for dir in lacalDir:
            path = osp.join(dirPath, dir)
            if osp.isdir(path):
                testList.addItem(dir)
    

This will add only directories to the listWidget ignoring the files.

  1. If you want to get the access time for the files and/or folders, use time following method:

    import os.path as osp
    accessTime = osp.getatime("path/to/dir") # returns the timestamp
    

Get access time for all the directories and one which has the greatest value is the latest accessed directory. This way you can get the latest accessed 3 directories.

qurban
  • 3,885
  • 24
  • 36
  • I could not understand the 2nd Q. Can you explain it further? The listWidget now contains 5 directories, how do you want to display 3 recent directories? – qurban Jan 20 '14 at 17:27
  • yes there are 5 folders in the directory but I wanted to display the recent 3 folders that has been modified, and hence hiding the other 2.. Does that make any sense? – dissidia Jan 23 '14 at 03:47
0

Maybe you should try "QFileDialog" like the following:

class MyWidget(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        fileNames = QFileDialog.getExistingDirectory(self, "list dir", "C:\\",QFileDialog.ShowDirsOnly)
        print fileNames

if __name__ == "__main__":
    app = QApplication(sys.argv)
    widget = MyWidget()
    widget.show()
    app.exec_()

2nd question, you could reference to this: enter link description here

charlie cui
  • 146
  • 4