Create File Explorer in Python using Tkinter

Python course with 57 real-time projects - Learn Python

In this Python project, we will build a GUI-based File Explorer using the Tkinter, OS, and shutil modules of Python. It is a beginner-level project, and you will need some brief knowledge about all the libraries and with this project, you will get to apply them in real life. Let’s get started!😊

About File Managers:

A file manager is a software program that helps the user manage all kinds of files in a computer. The most common type of file manager is the File Explorer, which is present in all the Operating Systems.

About the python file explorer project:

The objective of this is to create a GUI-based File Manager. To build this, you will need basic understanding of Tkinter, OS, and shutil libraries.

Project Prerequisites:

To build this python file explorer project, we will need the following libraries:

1. Tkinter – To create the GUI window.
2. OS – To perform operations on a file and it’s path.
3. shutil – To copy or move a file.

All the libraries come pre-installed with Python.

Download File Explorer Python Project

Please download the source code of python file explorer: File Explorer Python Code

Project File Structure

Here are the steps you will need to execute to build this python file explorer project:

1. Importing the necessary modules.
2. Defining the functions of all the buttons and defining certain variables for the GUI window.
3. Initializing the window, placing the components in it, and finalizing it.

Let’s take a closer look at these steps:

1. Importing all the necessary modules:

from tkinter import *
import tkinter.filedialog as fd
import tkinter.messagebox as mb

import os
import shutil

Explanation:

  • The from tkinter import * statement is used to import everything from the init file in the tkinter library.
  • The filedialog module is a collection of open and save dialog functions.
  • The messagebox module is a collection of functions that display different kinds of messages, with different icons.

2. Defining the functions of all the buttons and defining certain variables for the GUI window:

# Creating the backend functions for python file explorer project
def open_file():
   file = fd.askopenfilename(title='Choose a file of any type', filetypes=[("All files", "*.*")])

   os.startfile(os.path.abspath(file))


def copy_file():
   file_to_copy = fd.askopenfilename(title='Choose a file to copy', filetypes=[("All files", "*.*")])
   dir_to_copy_to = fd.askdirectory(title="In which folder to copy to?")

   try:
       shutil.copy(file_to_copy, dir_to_copy_to)
       mb.showinfo(title='File copied!', message='Your desired file has been copied to your desired location')
   except:
       mb.showerror(title='Error!', message='We were unable to copy your file to the desired location. Please try again')


def delete_file():
   file = fd.askopenfilename(title='Choose a file to delete', filetypes=[("All files", "*.*")])

   os.remove(os.path.abspath(file))

   mb.showinfo(title='File deleted', message='Your desired file has been deleted')


def rename_file():
   file = fd.askopenfilename(title='Choose a file to rename', filetypes=[("All files", "*.*")])

   rename_wn = Toplevel(root)
   rename_wn.title("Rename the file to")
   rename_wn.geometry("250x70"); rename_wn.resizable(0, 0)

   Label(rename_wn, text='What should be the new name of the file?', font=("Times New Roman", 10)).place(x=0, y=0)

   new_name = Entry(rename_wn, width=40, font=("Times New Roman", 10))
   new_name.place(x=0, y=30)

   new_file_name = os.path.join(os.path.dirname(file), new_name.get()+os.path.splitext(file)[1])

   os.rename(file, new_file_name)
   mb.showinfo(title="File Renamed", message='Your desired file has been renamed')


def open_folder():
   folder = fd.askdirectory(title="Select Folder to open")
   os.startfile(folder)


def delete_folder():
   folder_to_delete = fd.askdirectory(title='Choose a folder to delete')
   os.rmdir(folder_to_delete)
   mb.showinfo("Folder Deleted", "Your desired folder has been deleted")


def move_folder():
   folder_to_move = fd.askdirectory(title='Select the folder you want to move')
   mb.showinfo(message='You just selected the folder to move, now please select the desired destination where you want to move the folder to')
   destination = fd.askdirectory(title='Where to move the folder to')

   try:
       shutil.move(folder_to_move, destination)
       mb.showinfo("Folder moved", 'Your desired folder has been moved to the location you wanted')
   except:
       mb.showerror('Error', 'We could not move your folder. Please make sure that the destination exists')


def list_files_in_folder():
   i = 0

   folder = fd.askdirectory(title='Select the folder whose files you want to list')

   files = os.listdir(os.path.abspath(folder))

   list_files_wn = Toplevel(root)
   list_files_wn.title(f'Files in {folder}')
   list_files_wn.geometry('250x250')
   list_files_wn.resizable(0, 0)

   listbox = Listbox(list_files_wn, selectbackground='SteelBlue', font=("Georgia", 10))
   listbox.place(relx=0, rely=0, relheight=1, relwidth=1)

   scrollbar = Scrollbar(listbox, orient=VERTICAL, command=listbox.yview)
   scrollbar.pack(side=RIGHT, fill=Y)

   listbox.config(yscrollcommand=scrollbar.set)

   while i < len(files):
       listbox.insert(END, files[i])
       i += 1


# Defining the variables
title = 'DataFlair File Manager'
background = 'Yellow'

button_font = ("Times New Roman", 13)
button_background = 'Turquoise'

Explanation:

  • The following are the components of the filedialog module used in this project:
    • The askfilename function is used to select a file, with title title, and filetypes being filetypes.
    • The askdirectory function is used to select a folder.
    • Both these functions store the file in the form of its path.
  • The following are the components the os library used in this project:
    • Functions in the path module:
      • The abspath function is used to get the path of the file specified to it as an argument.
      • The join function is used to join two halves of a file, generally the name and the extension.
      • The dirname function is used to get the name of a file’s directory where it is placed.
      • The splittext function is used to split the name of a file in the form of a list containing its name and extensions as different items.
    • Functions in the init module:
      • The rename function is used to rename a file or folder.
      • The startfile function is used to open a file in an external application.
      • The rmdir function is used to delete an entire directory.
      • The remove function is used to delete a file.
  • The elements of the shutil library used in this project are:
    • The copy function is used to copy a file from its source location, src, to the desired destination, dst on the computer.
    • The move function is used to move a file from the source location, source, to the destination, destination.
      Note: All the tkinter elements in this step will be explained in the next one.

3. Initializing the window, placing the components, and finalizing it:

# Initializing the window
root = Tk()
root.title(title)
root.geometry('250x400')
root.resizable(0, 0)
root.config(bg=background)

# Creating and placing the components in the window
Label(root, text=title, font=("Comic Sans MS", 15), bg=background, wraplength=250).place(x=20, y=0)

Button(root, text='Open a file', width=20, font=button_font, bg=button_background, command=open_file).place(x=30, y=50)

Button(root, text='Copy a file', width=20, font=button_font, bg=button_background, command=copy_file).place(x=30, y=90)

Button(root, text='Rename a file', width=20, font=button_font, bg=button_background, command=rename_file).place(x=30, y=130)

Button(root, text='Delete a file', width=20, font=button_font, bg=button_background, command=delete_file).place(x=30, y=170)

Button(root, text='Open a folder', width=20, font=button_font, bg=button_background, command=open_folder).place(x=30, y=210)

Button(root, text='Delete a folder', width=20, font=button_font, bg=button_background, command=delete_folder).place(x=30, y=250)

Button(root, text='Move a folder', width=20, font=button_font, bg=button_background, command=move_folder).place(x=30, y=290)

Button(root, text='List all files in a folder', width=20, font=button_font, bg=button_background,
      command=list_files_in_folder).place(x=30, y=330)

# Finalizing the window
root.update()
root.mainloop()

Explanation:

  • The Tk() class is used to assign the GUI window to a variable.
    • The .title() method is used to give a title to the window.
    • The .geometry() method is used to define an initial geometry to the window.
    • The .resizable() method is used to give the user the permission to resize a window or not.
    • The .config() method is used to configure some extra attributes to the object.
  • The Label class adds a Label widget to the window which is a frame used to add static text to the window. It’s methods and attributes are:
    • The master attribute specifies the parent window of the widget.
    • The text attribute specifies the text to be displayed in the widget.
    • The font attribute is used to designate the font name, font size and bold/italic to the text.
    • The bg attribute gives a background color to the widget.
    • The wraplength attribute is used to define after how many pixels will the text be wrapped to another line.
  • The Button class adds a button to its parent element that executes a function when it is pressed. It’s attributes are:
    • The width attribute is used to specify the length of the widget, in pixels.
    • The command attribute is used to designate the function that will be executed when the button is pressed.
    • The other attributes have been discussed before.
  • The Toplevel class is an alternative to the Frame widget, except it always contains its child widgets in a new window.
    • All the methods and attributes of this class are the same as the Tk() class.
  • The Entry class is used to add an input field in the GUI window. It’s methods are:
    • The textvariable attribute is used to set a StringVar(string), IntVar(integer) or DoubleVar(float) variable to store or change the value in the Entry widget and to give it a type [as mentioned in the brackets against the __Var words].
    • The other attributes of this class have already been discussed before.
    • The .get() method is used to get the input that was entered by the user. This method takes no arguments.
  • The Listbox class is used to add a static, multiline textbox to the window from which the user can select items from.
    • The selectbackground attribute is used to specify the color of the background that has been selected.
    • The yscrollcommand attribute is used to set the scrollbar that will control the up-down scrolling of the widget.
    • The other attributes have been discussed before.
    • The .insert(index, *elements) method is used to insert an element or elements, *elements, at the index index.
    • The index argument can be a number, or an acceptable tkinter constant.
    • The .yview() method is used to set a scrollbar to view the up and down of the widget it is used on.
  • The Scrollbar class is used to add a scrollbar to its parent widget that controls the movement of the parent.
    • The orient attribute is used to indicate how the scrollbar will be displayed on the window.
    • The other attributes have been discussed before.
    • The .set() method is used to connect the scrollbar to another widget.

Python File Explorer Output

python file explorer output

Summary

Congratulations! You have now created your own File Manager project using the Tkinter, OS, and shutil modules of Python. You can use this project to manage some files on your computer, and use it to open, move, delete, rename or copy the file and even open, delete, rename and list all files in a folder.

Have fun coding!😊

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

follow dataflair on YouTube

2 Responses

  1. theo says:

    os.startfile(os.path.abspath(file))
    AttributeError: module ‘os’ has no attribute ‘startfile’

  2. theo says:

    os.startfile(os.path.abspath(file))
    AttributeError: module ‘os’ has no attribute ‘startfile’

    my code can’t run open file

Leave a Reply

Your email address will not be published. Required fields are marked *