Python File Explorer Project with Source Code

FREE Online Courses: Click for Success, Learn for Free - Start Now!

File explorer / manager is available on every laptop or computer we use. It helps to access files with the help of a graphical user interface. In this project, we will be creating a file explorer. Let’s start developing this Python File Manager project.

About File Explorer Project

In this project, we will develop a file explorer that will help us to save, open, rename, delete and move files and will also help in creating and deleting the folder.

Python File Explorer Project

The objective of this Python File Manager Project is to create our own File Explorer. Install tkinter and easygui to start developing the project.

Project Prerequisites

Basic knowledge of tkinter and easygui is required to develop this project. Furthermore, knowledge of functions is also required.

Download Python File Explorer

Download source code of python file explorer / manager: Python File Explorer Project Source Code

Project File Structure

1. Installing tkinter and easygui
2. Importing libraries
3. Save the File
4. Open the File
5. Renaming the file
6. Deleting a file
7. Copying a file
8. Deleting a folder
9. Creating a folder
10. Moving a folder
11. Initializing window and creating buttons

1. Installing tkinter and easygui:

To start developing the project, installation of these two modules is required. To install these modules write the following command on command prompt or terminal window.

Pip install tkinter
pip install easygui

2. Importing libraries

from tkinter import filedialog
from tkinter import *
import easygui
import os
from tkinter import messagebox as mb

Code Explanation:

a. tkinter: It is the most used Graphical User Interface package in python.
b. filedialog: While working with files, filedialog provides a set of dialogs.
c. easygui: It is the most easy Graphical User Interface module. This module is not event driven.
d. os: When different files are processed from different locations.
e. messagebox: Message boxes are displayed with the help of messagebox.

3. Save the File

def Save():
    def SaveAs():
        FileName = filedialog.asksaveasfile(initialdir="/",defaultextension='.txt',filetypes=[("text files",".txt"),("all files",".*")])
        FileText=str(textspace.get(1.0,END))
        FileName.write(FileText)
    Screen.destroy()
    SaveWindow= Tk()
    button = Button(text="SaveAs", command=SaveAs)
    button.pack()
    textspace = Text(SaveWindow)
    textspace.pack()

Code Explanation:

Save function displays a text area where we can enter the text that is to be saved. SaveAs function saves the file.
a. destroy: It is used to destroy a widget.
b. Tk(): We create Tk() to initialise the tkinter module.
c. pack(): It organizes widgets in blocks. After this they are placed in the parent widget.
d. filetypes: It contains the type of the file.

4. Open the File:

def Open():
    Read=easygui.fileopenbox()
    try:
        os.startfile(Read)
    except:
        mb.showinfo("file not found")

Code Explanation:

a. fileopenbox(): It is used to display the files. Only those files are displayed whose path matches the default file path.
b. startfile(): It opens the file.
c. showinfo(): It is used to display the information in the form of a pop up.

5. Renaming the file:

def Rename():
    Read=easygui.fileopenbox()
    pathnew = os.path.dirname(Read)
    extension=os.path.splitext(Read)[1]
    print("Enter new name of the file")
    newName=input()
    path1 = os.path.join(pathnew, newName+extension)
    print(path1)
    os.rename(Read,path1) 
    mb.showinfo("File Renamed !")

Code Explanation:

a. splittext(): This widget splits the path into ext and pair root.
b. showinfo(): It displays the text on the screen.
c. join(): It joins two different texts into one string.

6. Deleting a file:

def Delete():
    Read=easygui.fileopenbox()
    if os.path.exists(Read):
        os.remove(Read)             
    else:
        mb.showinfo("File not found , please check!")

Code Explanation:
a. exists(): It checks whether the path mentioned exists or not.
b. remove(): The given object is removed from the list with this widget.

7. Copying a file:

def Copy():
    Read=easygui.fileopenbox()
    destination1=filedialog.askdirectory()
    shutil.copy(Read,destination1)
    mb.showinfo("File successfully copied ")

Code Explanation:

a. shutil: Various operations related to files are offered in this module.

8. Deleting a folder:

def DeleteFolder():
    DelFolder = filedialog.askdirectory()
    os.rmdir(DelFolder)
    mb.showinfo("Folder successfully deleted")

Code Explanation:
a. rmdir(): Removal of an empty folder is done with this widget.
b. askdirectory(): Prompting the user to select a directory.

9. Creating a folder:

def CreateFolder():
    Folder = filedialog.askdirectory()
    print("Enter a name for the folder")
    NewFolder=input()
    path = os.path.join(Folder, NewFolder)  
    os.mkdir(path)
    mb.showinfo("Folder created successfully")

Code Explanation:

a. input(): It takes input from the user.
b. mkdir(): New directory with a new path is created with this widget.

10. Moving a folder:

def MoveFile():
    Read=easygui.fileopenbox()
    Destination =filedialog.askdirectory()
    if(Read==Destination):
        mb.showinfo('confirmation', "Source and destination are same")
    else:
        shutil.move(Read, Destination)  
        mb.showinfo("File has moved  successfully")

Code Explanation:

a. move(): A folder can be moved to another location with this widget.

11. Initializing window and creating buttons:

Screen=Tk()
Screen.title("File Explorer by - ProjectGururkul ")
Screen.geometry("400x400")
Screen.config(bg="yellow")
SaveButton = Button(text="Save",command=Save)
SaveButton.place(relx=0.3,rely=0.2)
OpenButton = Button(text="Open",command=Open)
OpenButton.place(relx=0.5,rely=0.2)
RenameButton = Button(text="Rename",command=Rename)
RenameButton.place(relx=0.3,rely=0.4)
CopyButton = Button(text="Copy",command=Copy)
CopyButton.place(relx=0.5,rely=0.4)
DeleteButton = Button(text="Delete File",command=Delete)
DeleteButton.place(relx=0.3,rely=0.6)
DeleteFolderButton = Button(text="Delete Folder",command=DeleteFolder)
DeleteFolderButton.place(relx=0.5,rely=0.6)
CreateFolderButton = Button(text="Create Folder",command=Rename)
CreateFolderButton.place(relx=0.3,rely=0.8)
MoveFileButton = Button(text="Move File",command=MoveFile)
MoveFileButton.place(relx=0.5,rely=0.8)
mainloop()

Code Explanation:

a. title(): It sets the title of the main window.
b.geometry(): The dimensions of the window are set with this widget.
c. config(): It changes the property of the widget.
d. Button(): It adds the button on screen.
e. place(): It places the widget in the specific position of the screen.

Python File Explorer Output

python file explorer output

Summary

We have successfully developed python file explorer / manager project with basic tkinter libraries. This is a nice project for python beginners.

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google | Facebook

1 Response

  1. Deepa says:

    can you please post a code for paste attribute too

Leave a Reply

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