Tuesday, December 4, 2018

codingdirectional: List out all the files within a folder with python

In this article we will continue with our windows application development using tkinter, in the previous article we have created a button which when we click on it we can select a file from a folder and the program will print the name of that file on the pycharm command console. In this article we will write the program to do this.

1) Select a folder.
2) Print all the name of the files (with file extension) within that folder on the label part of our UI.

First we will edit the main file of the program so it will open up a folder instead of a file.

from tkinter import *
from tkinter import filedialog
from Remove import Remove

win = Tk() # 1 Create instance
win.title("Multitas") # 2 Add a title
win.resizable(0, 0) # 3 Disable resizing the GUI
win.configure(background='black') # 4 change background color

# 5 Create a label
aLabel = Label(win, text="Remove duplicate file", anchor="center")
aLabel.grid(column=0, row=1)
aLabel.configure(foreground="white")
aLabel.configure(background="black")

# 6 Create a selectFile function to be used by button
def selectFile():

    #filename = filedialog.askopenfilename(initialdir="/", title="Select file")
    folder = filedialog.askdirectory() # 7 open a folder then create and start a new thread to print those filenames from the selected folder
    remove = Remove(folder, aLabel) 
    remove.start()

# 8 Adding a Button
action = Button(win, text="Open Folder", command=selectFile)
action.grid(column=0, row=0) # 9 Position the button
action.configure(background='brown')
action.configure(foreground='white')

win.mainloop()  # 10 start GUI

As you can see, we have passed in the name of that folder which we have selected when we create a new Remove thread instance in the selectFile method, besides that we have also passed the label object into the Remove thread instance so we can modify it’s content later on.

Here is the modify version of the Remove thread class.

import threading
import os

class Remove(threading.Thread):

   def __init__(self, massage, aLabel):

      threading.Thread.__init__(self)
      self.massage = massage
      self.label = aLabel

   def run(self):

      text_filename = ''
      filepaths = os.listdir(self.massage)
      for filepath in filepaths:
         text_filename += filepath + '\n'
      self.label.config(text=text_filename)
      return

The program is very simple, get each file name from that selected directory, concatenates them and finally prints them on the label object.

Open a file folderOpen a file folder

Our main objective here is to create a program which will remove the duplicate files in another folder, we have already known the method to open a single file, and in this article we have learned the method to open a folder, in the next chapter we will continue to add in more methods for this particular program.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...