1

必要なパスに存在するディレクトリとファイルのリストを表示するために、python と PyQt5 ライブラリを使用しています。

ユーザーが目的のフォルダーを選択すると、プログラムはすべての既存のフォルダーとサブフォルダーとファイルを保持するリストを作成し、すべてのフォルダーを表示するためにこのリストを QlistWidget に追加します。

ここに画像の説明を入力

私が欲しいのは、このリストを TreeList に変換して、次のように表示することです:

folder1 
   subfolder1
      file1
      file2
folder2
   subfolder1
      file1
      file2
      file3
   subfolder2
      file1

機能は次のとおりです。

def checkPath(self,folder):
        fileList=[]    
        try:
            directory=folder

            '''
             check if entered path exist 
             sleep for 2 seconds
            '''
            if os.path.exists(folder):
                print("{0} is a valid Path".format(folder))
                time.sleep(2)
                self.listWidgetPDFlist.clear()
                '''
                 looping over the path  using os,walk 
                ==> filter the PDF files then join the path entered with the fileName 
                ==> append the filtered files into a list  in order to apply list functions.
                '''
                for root,dirs,files in os.walk(directory):

                    for filename in files:
                        if filename.endswith(self.lineEdit_Ext.text()):

                            t=os.path.join(root,filename)

                            print(t)
                            fileList.append(t)

                # add the list into the  listWidgetPDFlist          
                self.listWidgetPDFlist.addItems(fileList)

                # get the total number of existing  PDF files in the requested path
                totalPDFNum=len(fileList)

                '''
                check if the length of the list if <= 0 
                yes ==> no PDF files were founded  TOTAL = 0 
                no ==>  PDF files were founded  TOTAL = totalPDFNum
                '''
                if(totalPDFNum <= 0 ):
                    print("{0} path doesn't includes any {1} files ".format(directory,self.lineEdit_Ext.text()))
                    self.lineEditTotalPDFnumber.setText(str(totalPDFNum))

                else:    
                    self.lineEditTotalPDFnumber.setText(str(totalPDFNum))
                    print ("\nthe ToTal Number of files  = {0} ".format(totalPDFNum) )        
                return folder

            #if entered path doesn't exist      
            else:    
                print("{0}is not a valid Path".format(folder))
                return False

        except Exception as e:
            print("this error occure {0}".format(e))
4

2 に答える 2