Python File Manager- Find Specific Files or Folders

Rahul Patodi
Wiki Flood
Published in
5 min readDec 5, 2023

A File Manager is a software or application that helps users to manage files or folders on the device. File Manager allows the user to view, edit, copy, delete, or move files and folders.

About Python File Manager Project:

The objective of this project is to create a File Manager in Python with the help of tkinter, os and shutil modules. With this File Manager, users can view, edit, copy, delete, or move files and folders.

Prerequisites For Python File Manager Project:

First we will install the required library and modules in our system using pip installer.


pip install tkinter
pip install shutil
pip install os
Python File Manager
Python File Manager

Importing the required Library and Modules:

from tkinter import *
import shutil
import os
from tkinter import filedialog,messagebox

tkinter- This library will help us in creating a GUI window for our app.

shutil- This module helps in copying and moving files or folders in the system.

os- This module helps in interacting with the operating system.

messagebox- This will create a pop-up message window.

Initializing GUI Window:

root=Tk()
root.geometry("400x330")
root.resizable(False,False)
root.title("File Manager")
root.config(bg='#080808')

root- It is the name of our GUI window.

Tk()- It initialises tkinter which means GUI window is created.

geometry()- This method provides the length and breadth to the GUI window.

resizeable()- This method allows the window to change its size as per user need.

title()- This method gives title to the window

confg()- This method sets the configuration of the window.

Creating Label and Buttons:

Label(root,text="File Manager",font=('Arial,bold',25),bg='#03A89E',fg='Black',width=30).pack(pady=10)

Label()- It is used to display one line or more than one line of text.

text- It is used to display text on labels.

font- It is a style in which font is written.

bg- It is the background Colour of the label.

pack()- This displays the widgets in the GUI window.

Button(root,text="Open File",font=('Arial,bold',15),bd=5,bg='Turquoise',command=openfile).place(x=42,y=80)
Button(root,text="Copy File",font=('Arial,bold',15),bd=5,bg='Turquoise',command=copyfile).place(x=42,y=140)
Button(root,text="Rename File",font=('Arial,bold',15),bd=5,bg='Turquoise',command=renamefile).place(x=30,y=260)
Button(root,text="Delete File",font=('Arial,bold',15),bd=5,bg='Turquoise',command=deletefile).place(x=37,y=200)
Button(root,text="Open Folder",font=('Arial,bold',15),bd=5,bg='Turquoise',command=openfolder).place(x=215,y=80)
Button(root,text="Delete Folder",font=('Arial,bold',15),bd=5,bg='Turquoise',command=deletefolder).place(x=210,y=200)
Button(root,text="Move Folder",font=('Arial,bold',15),bd=5,bg='Turquoise',command=movefolder).place(x=215,y=140)
Button(root,text="List files in folders",font=('Arial,bold',15),bd=5,bg='Turquoise',command=listfilesin_folders).place(x=190,y=260)


root.mainloop()

Button()- It is a button used to display on our window.

bd- It is the border of the Button.

command- It is used as a function of a button when it is clicked.

place()- It is used to set the position.

root.mainloop()- It is simply a method in the main window that executes what we wish to execute in an application and ends the mainloop.

Defining Functions:

Function to Open a File-

def openfile():
file=filedialog.askopenfilename(title="Select file",filetypes=[("All files","*.*")])
os.startfile((os.path.abspath(file)))

def openfile()- This function is created to open a file.

filedialog.askopenfilename()- This will help in selecting the file.

os.startfile()- This will open the selected file.

Function to Copy a File-

def copyfile():
c_file=filedialog.askopenfilename(title="Select file to copy",filetypes=[("All files","*.*")])
p_file=filedialog.askdirectory(title="Select folder to to paste file?")
try:
shutil.copy(c_file,p_file)
messagebox.showinfo(title="File Copied",message="The File has been copied")
except:
messagebox.showerror(title="Error",message="Unable to copy file")

def copyfile()- This function is created to copy the file.

filedialog.askopenfilename()- This will help in selecting the file.

filedialog.askdirectory()- This will help in selecting the folder to paste file in it.

shutil.copy()- This will copy the selected file and paste it in the desired folder..

messagebox.showinfo,showerror- This will display the message of File copied and error.

Function to Delete a File-

def deletefile():
file=filedialog.askopenfilename(title="Select file to delete",filetypes=[("All files","*.*")])
os.remove(os.path.abspath(file))
messagebox.showinfo(title="File deleted",message="File has been deleted")

def deletefile()- This function is created to delete the file.

filedialog.askopenfilename()- This will help in selecting the file.

os.remove()- This will delete the selected file.

Function to Open a Folder-

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

def openfolder()- This function will open the folder.

Function to Delete a Folder-

def deletefolder():
del_folder= filedialog.askdirectory(title="Select Folder to Delete")
os.rmdir(del_folder)
messagebox.showinfo("Folder Deleted")

def deletefolder()- This function is created to delete the folders.

os.rmdir()- This will delete the selected folder.

Function to Move a Folder-

def movefolder():
move_folder=filedialog.askdirectory(title="Select Folder to move")
messagebox.showinfo(message="Folder has been selected to move! Now select the desired destination")
destination=filedialog.askdirectory(title="Destination")
try:
shutil.move(move_folder,destination)
messagebox.showinfo("Folder Moved!","The Folder has been moved to desired destination")
except:
messagebox.showinfo("Error!!","Unable to move Folder.Please make sure that the destination exists")

def movefolder()-This function is created to move the folder.

shutil.move()-This will move the selected folder and paste it in the desired location.

Function to List files in a Folder-

def listfilesin_folders():
i=0
folder=filedialog.askdirectory(title="Select Folder")
files=os.listdir(os.path.abspath(folder))
listfiles_win=Toplevel(root)
listfiles_win.title(f"Files in {folder}")
listfiles_win.geometry("250x250")
listbox=Listbox(listfiles_win,font=('Arial,bold',15))
listbox.place(relx=0,rely=0,relheight=1,relwidth=1)


scrollb=Scrollbar(listbox,orient=VERTICAL,command=listbox.yview)
scrollb.pack(side=RIGHT,fill=Y)
listbox.config(yscrollcommand=scrollb.set)
while i<len(files):
listbox.insert(END,files[i])
i+=1

Here we have created a pop-up window which displays the list of files from the selected folder.

def listfiles_folders()- This function is created to list the files.

os.listdir()- This will make the list of files in the selected folder.

Listbox()- This will create a list box which displays the list of files.

Scrollbar()- This will add a scrollbar in the listbox to slide it up and down.

orient()- This will set the orient of the scrollbar in Vertical.

Function to Rename a File-

def renamefile():
renamewin=Toplevel(root)
renamewin.title("rename the file")
renamewin.geometry("200x100")
rlabel=Label(renamewin,text="Enter the file Name",textvariable=fileNameEntered,font=('Arial,bold',15))
rlabel.pack()
rent=Entry(renamewin,font=('Arial,bold',15))
rent.pack()
btn=Button(renamewin,text="Submit",command=submitname)
btn.pack()


def showfilepath():
files= filedialog.askopenfilename(title="Select the file to Rename", filetypes=[("All files","*.*")])
return files


def submitname():
renameName=fileNameEntered.get()
fileNameEntered.set("")
filename=showfilepath()
newfileName=os.path.join(os.path.dirname(filename),renameName+os.path.splitext(filename)[1])
os.rename(filename,newfileName)
messagebox.showinfo(title="File Renamed",message="File name has been changed")

def renamefile()- This function is created to rename a file.

Here we are creating a new window for renaming the file.

def showfilepath()- This function is created to show the path of file.

file.

filedialog.askopenfilename()- This will help in selecting the file.

def submitname()- This function is created to remove the old name and add a new name to the file.

get()- This will retrieve the name from the entry field.

os.path.join()- This will add the directory path with the new name of the file.

Python File Manager Output-

1. Open File:

Open File

2. Open Folder:

Open Folder
Open Folder

3. Copy File:

Copy File
Copy File

4. Move Folder:

Move Folder

5. Delete File:

Delete File
Delete File

6. Delete Folder:

Delete Folder
Delete Folder

7. Rename File:

Rename File
Rename File

8. List files in folders:

List files in folders
List files in folders

Conclusion

We have successfully created a File Manager for our system using Python. In this Python file manager project, we learned how to use tkinter for making the interface of the application.

--

--