Ad

How to make a File Manager with python(tkinter)

Python File Manager Project with Source Code

File 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 manager. Let’s start developing this Python File Manager project.

About This File Manager Project

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

Python File manager Project

The objective of this Python File Manager Project is to create our own File  Manager. 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.

Table of contents:

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

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

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

#Saving a 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()

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:

#opening a file
def Open():
    Read=easygui.fileopenbox()
    try:
        os.startfile(Read)
    except:
        mb.showinfo("file does not exist")

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:

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

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:

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

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:

#copying a file
def Copy():
    Read=easygui.fileopenbox()
    destination1=filedialog.askdirectory()
    shutil.copy(Read,destination1)
    mb.showinfo("copied ")

Explanation:

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

8. Deleting a folder:

#deleting a folder
def DeleteFolder():
    DelFolder = filedialog.askdirectory()
    os.rmdir(DelFolder)
    mb.showinfo("Folder deleted")

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:

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

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:

#Moving the file
def MoveFile():
    Read=easygui.fileopenbox()
    Destination =filedialog.askdirectory()
    if(Read==Destination):
        mb.showinfo('confirmation', "fil already exist")
    else:
        shutil.move(Read, Destination)  
        mb.showinfo("File moved successfully")

Explanation:

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

11. Initializing  the canvas and creating buttons:

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

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 Manager Output

This is the whole source code joined together:

import os
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox as mb
import easygui
import shutil
#Saving a 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()
#opening a file
def Open():
    Read=easygui.fileopenbox()
    try:
        os.startfile(Read)
    except:
        mb.showinfo("file does not exist")
#Renaming a file
def Rename():
    Read=easygui.fileopenbox()
    pathnew = os.path.dirname(Read)
    extension=os.path.splitext(Read)[1]
    print("Enter name of the file")
    newName=input()
    path1 = os.path.join(pathnew, newName+extension)
    print(path1)
    os.rename(Read,path1)
    mb.showinfo("File Renamed !")
#deleting a file
def Delete():
    Read=easygui.fileopenbox()
    if os.path.exists(Read):
        os.remove(Read)            
    else:
        mb.showinfo("File not found , please check!")
#copying a file
def Copy():
    Read=easygui.fileopenbox()
    destination1=filedialog.askdirectory()
    shutil.copy(Read,destination1)
    mb.showinfo("copied ")
#deleting a folder
def DeleteFolder():
    DelFolder = filedialog.askdirectory()
    os.rmdir(DelFolder)
    mb.showinfo("Folder deleted")
#creating a folder
def CreateFolder():
    Folder = filedialog.askdirectory()
    print("Enter folder name")
    NewFolder=input()
    path = os.path.join(Folder, NewFolder)  
    os.mkdir(path)
    mb.showinfo("Folder created successfully")
#Moving the file
def MoveFile():
    Read=easygui.fileopenbox()
    Destination =filedialog.askdirectory()
    if(Read==Destination):
        mb.showinfo('confirmation', "fil already exist")
    else:
        shutil.move(Read, Destination)  
        mb.showinfo("File moved successfully")
#creating buttons and Initializing window
Screen=Tk()
Screen.title("File Explorer by - Mad Programmer ")
Screen.geometry("600x400")
Screen.config(bg="teal")
SaveButton = Button(text="Save",command=Save, bg="orange")
SaveButton.place(relx=0.3,rely=0.2)
OpenButton = Button(text="Open",command=Open, bg="orange")
OpenButton.place(relx=0.5,rely=0.2)
RenameButton = Button(text="Rename",command=Rename, bg="orange")
RenameButton.place(relx=0.3,rely=0.4)
CopyButton = Button(text="Copy",command=Copy, bg="orange")
CopyButton.place(relx=0.5,rely=0.4)
DeleteButton = Button(text="Delete File",command=Delete, bg="orange")
DeleteButton.place(relx=0.3,rely=0.6)
DeleteFolderButton = Button(text="Delete Folder",command=DeleteFolder, bg="orange")
DeleteFolderButton.place(relx=0.5,rely=0.6)
CreateFolderButton = Button(text="Create Folder",command=Rename, bg="orange")
CreateFolderButton.place(relx=0.3,rely=0.8)
MoveFileButton = Button(text="Move File",command=MoveFile, bg="orange")
MoveFileButton.place(relx=0.5,rely=0.8)
mainloop()


Summary

We have successfully developed python file manager project with basic tkinter libraries, you can copy this source code and add more features to it. If you have any question hit me up in the comments.

Happy coding.

Merry Christmas in advance.

 

Post a Comment

0 Comments

sponsored