Linking certain filenames in a directory and a dictonary?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kase
    New Member
    • Aug 2010
    • 2

    Linking certain filenames in a directory and a dictonary?

    Hi. I'm trying to get my feet wet with programming and keep hitting some brick walls.

    I basically want to look in a folder for certain filenames. If the files are there, I would like the term linked to the filename to be in a listbox.

    I thought a dictionary would work:
    Code:
    dataset = { "File1.txt":"Picotiter Plate",
                        "File2.txt":"KeyPass Wells",
                        "File3.txt":"Reagent Lot numbers",
                        "File4.txt":"Insert other trackable item"}
    Basically, a user selects a certain folder (using tkFileDialog.as kdirectory(). I then need a check to see if the filenames (the first part of the dictionary) are present. Then I would like the second part of the dictionary to be in the listbox.

    I'm using tkinter.


    Here's what I have:
    Code:
    #! /usr/bin/env python
    from Tkinter import *
    import tkMessageBox
    import tkFileDialog
    import os
    
    class FileHandling:
    
        def datasets(self):
              dataset = { "File1.txt":"Picotiter Plate",
                        "File2.txt":"KeyPass Wells",
                        "File3.txt":"Reagent Lot numbers",
                        "File4.txt":"Insert other trackable item"}
              
    
    class GUIFramework(Frame):
        """This is the GUI"""
        
        def __init__(self,master=None):
            """Initialize yourself"""
            
            """Initialise the base class"""
            Frame.__init__(self,master)
            
            """Set the Window Title"""
            self.master.title("454 Run Tracer")
            """Display the main window
            with a little bit of padding"""
            self.grid(padx=15, pady=15,sticky=N+S+E+W)
            self.CreateWidgets()
    
        def pathentry(self):
            path = tkFileDialog.askdirectory()
            path_selectedFolder = "%s" % path
            print "You chose %s" % path
            for file in os.listdir("%s" % path):
                self.FileList.insert(END, file)
            if FileHandling.dataset() in os.listdir("%s" %path): #Having trouble #here
               "" self.DataTypes.insert(END, WHAT GOES HERE?""") #Trouble here #also
                                                                         
    
        
           
        def CreateWidgets(self):
            
    
            """Create the buttons """
            self.Instructions = Label(self, text="First, select a folder --->")
            self.Instructions.grid(row=0, column=0)
            self.btnDirectory = Button(self, text= "Select Run Folder", fg= 'blue', command=self.pathentry)
            self.btnDirectory.grid(row =0, column = 2, columnspan= 4, sticky = E+W)
            self.btnSelectAll = Button(self, text="Select All", fg='blue') #command = self.select_all
            self.btnSelectAll.grid(row=8, column = 2)
            self.btnCollectData = Button(self, text = "Collect Data" ,fg= "blue") # command = self.CollectData)
            self.btnCollectData.grid(row =8, column = 3)
    
    
            """Create a frame to separate the buttons from the listbox"""
            spaceframe = Frame(self, height= 5)
            spaceframe.grid(row=4, column = 1)
            
            self.FileListLabel = Label(self, text= "Select Data to Collect: ")
            self.FileListLabel.grid(row = 3, column = 0)
            """Create the Listbox Widget"""
            scrollbarV = Scrollbar(self, orient = VERTICAL)
            scrollbarH = Scrollbar(self, orient = HORIZONTAL)
            self.FileList= Listbox(self, selectmode= MULTIPLE
                                , width= 40
                                , yscrollcommand=scrollbarV.set
                                , xscrollcommand=scrollbarH.set
                                , relief= SUNKEN
                                , takefocus = 0
                                , borderwidth = 1
                                , state= NORMAL
                                , cursor = "arrow")
            self.FileList.grid(row=5, column = 0, columnspan = 7, sticky = N + W + S + E)
            scrollbarV.grid(row= 5, column = 7, sticky = N + S)
            scrollbarV.config(command = self.FileList.yview)
            scrollbarH.grid(row = 6, column = 0, columnspan = 10, sticky = E+W)
            scrollbarH.config(command= self.FileList.xview)
    
            self.DataTypes = Listbox(self, selectmode=MULTIPLE
                                     , width=40
                                     , yscrollcommand=scrollbarV.set
                                     , xscrollcommand=scrollbarH.set
                                     , relief=SUNKEN
                                     , takefocus= 0
                                     , borderwidth=1
                                     , state= NORMAL
                                     , cursor = "arrow")
            self.DataTypes.grid(row=5, column = 3, columnspan=6, sticky= N + W + S + E)
    
    if __name__ == "__main__":
        guiFrame = GUIFramework()
        guiFrame.mainloop()

    Any help would be very appreciated. I'm trying to clear out the cobwebs.

    Thanks
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    I modified some parts of the posted code to display the information you wanted. If the file name is not in the dictionary, the text "XXX" is displayed.

    Code:
    #! /usr/bin/env python
    from Tkinter import *
    import tkMessageBox
    import tkFileDialog
    import os
     
    class FileHandling:
        datasets = { "File1.txt":"Picotiter Plate",
                    "File2.txt":"KeyPass Wells",
                    "File3.txt":"Reagent Lot numbers",
                    "File4.txt":"Insert other trackable item"}
    
     
     
    class GUIFramework(Frame):
        """This is the GUI"""
     
        def __init__(self,master=None):
            """Initialize yourself"""
     
            """Initialise the base class"""
            Frame.__init__(self,master)
     
            """Set the Window Title"""
            self.master.title("454 Run Tracer")
            """Display the main window
            with a little bit of padding"""
            self.grid(padx=15, pady=15,sticky=N+S+E+W)
            self.CreateWidgets()
     
        def pathentry(self):
            path = tkFileDialog.askdirectory()
            print "You chose %s" % path
            fileList = os.listdir(path)
            for name in fileList:
                self.FileList.insert(END, name)
            dd = FileHandling.datasets
            for name in fileList:
                self.DataTypes.insert(END, dd.get(name, "XXX"))

    Comment

    • Kase
      New Member
      • Aug 2010
      • 2

      #3
      Awesome

      That worked! I modified it a little so that I didn't get a long list of 'XXX' based on what files were in the folder that didn't correlate to the dictionary.
      I think it works so far:
      Code:
       for name in fileList:
                  self.DataTypes.insert(END, dd.get(name))
      Thanks a ton!

      Comment

      Working...