Basic File Explorer
Abstract
Basic File Explorer is a graphical file browsing application built using Python’s Tkinter library. The application provides a simple and intuitive interface for users to navigate their file system, select files, and view the contents of text files. It features a clean GUI with buttons for browsing files, displaying file paths, and showing file contents in a text area. This project is excellent for beginners to learn GUI development, file handling, and event-driven programming in Python.
Prerequisites
- Python 3.6 or above
- A code editor or IDE
- tkinter module (usually comes pre-installed with Python)
Before you Start
Before starting this project, you must have Python installed on your computer. If you don’t have Python installed, you can download it from here. You must have a code editor or IDE installed on your computer. If you don’t have any code editor or IDE installed, you can download Visual Studio Code from here.
Note: Tkinter usually comes pre-installed with Python. If you encounter import errors, you may need to install it separately:
# For Ubuntu/Debian
sudo apt-get install python3-tk
# For CentOS/RHEL
sudo yum install tkinter
# For Ubuntu/Debian
sudo apt-get install python3-tk
# For CentOS/RHEL
sudo yum install tkinter
Getting Started
Create a Project
- Create a folder named
file-explorer
file-explorer
. - Open the folder in your favorite code editor or IDE.
- Create a file named
fileexplorer.py
fileexplorer.py
. - Copy the given code and paste it in your
fileexplorer.py
fileexplorer.py
file.
Write the Code
- Copy and paste the following code in your
fileexplorer.py
fileexplorer.py
file.
⚙️ Basic File Explorer
# Basic File Explorer in Python
import tkinter as tk # pip install tk
from tkinter import *
from tkinter import filedialog, Text
root = tk.Tk()
apps = []
def browseFiles():
output.delete('1.0', END)
filename = filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetypes = (("Text files","*.txt*"), ("all files", "*.*")))
pathh.config(text = "Path: " + filename)
tf = open(filename, encoding="utf8")
data = tf.read()
output.insert(END, data)
tf.close()
root.title("File Explorer")
root.geometry("700x700")
root.config(background="white")
label_file_explorer = Label(root, text = "File Explorer using Tkinter", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
button_explore = Button(root, text = "Browse Files", command = browseFiles)
exit_button = Button(root, text = "Exit", command = root.destroy)
output = Text(root)
pathh = Label(root, text = "Path: ", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
label_file_explorer.grid(column = 1, row = 1)
button_explore.grid(column = 1, row = 2)
exit_button.grid(column = 1, row = 3)
output.grid(column = 1, row = 4)
pathh.grid(column = 1, row = 5)
root.mainloop()
# Basic File Explorer in Python
import tkinter as tk # pip install tk
from tkinter import *
from tkinter import filedialog, Text
root = tk.Tk()
apps = []
def browseFiles():
output.delete('1.0', END)
filename = filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetypes = (("Text files","*.txt*"), ("all files", "*.*")))
pathh.config(text = "Path: " + filename)
tf = open(filename, encoding="utf8")
data = tf.read()
output.insert(END, data)
tf.close()
root.title("File Explorer")
root.geometry("700x700")
root.config(background="white")
label_file_explorer = Label(root, text = "File Explorer using Tkinter", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
button_explore = Button(root, text = "Browse Files", command = browseFiles)
exit_button = Button(root, text = "Exit", command = root.destroy)
output = Text(root)
pathh = Label(root, text = "Path: ", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
label_file_explorer.grid(column = 1, row = 1)
button_explore.grid(column = 1, row = 2)
exit_button.grid(column = 1, row = 3)
output.grid(column = 1, row = 4)
pathh.grid(column = 1, row = 5)
root.mainloop()
- Save the file.
- Open the terminal in your code editor or IDE and navigate to the folder
file-explorer
file-explorer
.
C:\Users\Your Name\file-explorer> python fileexplorer.py
# A GUI window will open with:
# - "File Explorer using Tkinter" title
# - "Browse Files" button
# - "Exit" button
# - Text area for displaying file contents
# - Path display area
C:\Users\Your Name\file-explorer> python fileexplorer.py
# A GUI window will open with:
# - "File Explorer using Tkinter" title
# - "Browse Files" button
# - "Exit" button
# - Text area for displaying file contents
# - Path display area
Explanation
- Import the required modules.
import tkinter as tk # pip install tk
from tkinter import *
from tkinter import filedialog, Text
import tkinter as tk # pip install tk
from tkinter import *
from tkinter import filedialog, Text
- Create the main window and initialize lists.
root = tk.Tk()
apps = []
root = tk.Tk()
apps = []
- Define the file browsing function.
def browseFiles():
output.delete('1.0', END)
filename = filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetypes = (("Text files","*.txt*"), ("all files", "*.*")))
pathh.config(text = "Path: " + filename)
tf = open(filename, encoding="utf8")
data = tf.read()
output.insert(END, data)
tf.close()
def browseFiles():
output.delete('1.0', END)
filename = filedialog.askopenfilename(initialdir = "/", title = "Select a File", filetypes = (("Text files","*.txt*"), ("all files", "*.*")))
pathh.config(text = "Path: " + filename)
tf = open(filename, encoding="utf8")
data = tf.read()
output.insert(END, data)
tf.close()
- Configure the main window.
root.title("File Explorer")
root.geometry("700x700")
root.config(background="white")
root.title("File Explorer")
root.geometry("700x700")
root.config(background="white")
- Create GUI components.
label_file_explorer = Label(root, text = "File Explorer using Tkinter", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
button_explore = Button(root, text = "Browse Files", command = browseFiles)
exit_button = Button(root, text = "Exit", command = root.destroy)
output = Text(root)
pathh = Label(root, text = "Path: ", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
label_file_explorer = Label(root, text = "File Explorer using Tkinter", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
button_explore = Button(root, text = "Browse Files", command = browseFiles)
exit_button = Button(root, text = "Exit", command = root.destroy)
output = Text(root)
pathh = Label(root, text = "Path: ", width = 100, height = 3, fg = "gray", bg = "whitesmoke")
- Arrange components using grid layout.
label_file_explorer.grid(column = 1, row = 1)
button_explore.grid(column = 1, row = 2)
exit_button.grid(column = 1, row = 3)
output.grid(column = 1, row = 4)
pathh.grid(column = 1, row = 5)
root.mainloop()
label_file_explorer.grid(column = 1, row = 1)
button_explore.grid(column = 1, row = 2)
exit_button.grid(column = 1, row = 3)
output.grid(column = 1, row = 4)
pathh.grid(column = 1, row = 5)
root.mainloop()
Features
- Graphical User Interface: Clean and intuitive Tkinter-based GUI
- File Browsing: Navigate through the file system using file dialog
- File Type Filtering: Filter files by type (text files, all files)
- File Content Display: View the contents of selected text files
- Path Display: Show the full path of the selected file
- UTF-8 Support: Handle files with various character encodings
- Exit Functionality: Clean application termination
How to Use
- Launch the Application: Run the Python script to open the file explorer window
- Browse Files: Click the “Browse Files” button to open the file selection dialog
- Select a File: Navigate through directories and select a file to open
- View Contents: The file’s content will be displayed in the text area
- Check Path: The full file path will be shown at the bottom
- Exit: Click the “Exit” button to close the application
GUI Components Breakdown
- Title Label: Displays the application name
- Browse Button: Opens the file selection dialog
- Exit Button: Closes the application
- Text Area: Shows the content of selected files
- Path Label: Displays the full path of the selected file
File Type Support
The current implementation supports:
- Text Files (.txt): Primary file type with dedicated filter
- All Files (.): Fallback option for any file type
Next Steps
You can enhance this project by:
- Adding support for more file types (PDF, Word, images)
- Implementing file editing capabilities
- Adding a file tree view for better navigation
- Creating file operations (copy, move, delete, rename)
- Adding syntax highlighting for code files
- Implementing search functionality within files
- Adding recent files history
- Creating bookmarks for frequently accessed folders
- Adding file properties display (size, date modified, etc.)
- Implementing drag and drop functionality
Error Handling Improvements
Consider adding these error handling features:
def browseFiles():
try:
output.delete('1.0', END)
filename = filedialog.askopenfilename(...)
if filename: # Check if file was selected
pathh.config(text = "Path: " + filename)
with open(filename, encoding="utf8") as tf:
data = tf.read()
output.insert(END, data)
except UnicodeDecodeError:
output.insert(END, "Error: Cannot decode file. File may be binary or use different encoding.")
except Exception as e:
output.insert(END, f"Error opening file: {str(e)}")
def browseFiles():
try:
output.delete('1.0', END)
filename = filedialog.askopenfilename(...)
if filename: # Check if file was selected
pathh.config(text = "Path: " + filename)
with open(filename, encoding="utf8") as tf:
data = tf.read()
output.insert(END, data)
except UnicodeDecodeError:
output.insert(END, "Error: Cannot decode file. File may be binary or use different encoding.")
except Exception as e:
output.insert(END, f"Error opening file: {str(e)}")
Conclusion
In this project, we learned how to create a Basic File Explorer using Python’s Tkinter library. We explored GUI development concepts including event handling, layout management, file dialogs, and text display widgets. This project demonstrates essential skills for desktop application development and provides a foundation for more complex file management applications. The combination of file handling and GUI programming makes this an excellent learning project for intermediate Python developers. To find more projects like this, you can visit Python Central Hub.
Was this page helpful?
Let us know how we did