0% found this document useful (0 votes)
9 views5 pages

Dhroov Assignment10

Bznxkckrnsaja

Uploaded by

dhruuvd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Dhroov Assignment10

Bznxkckrnsaja

Uploaded by

dhruuvd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No 10

Name: Atharva
Dhroov S. Humane
WaddDesai
Reg No: 221070028
221070076

Aim: Implement File System

Theory:

1. File System: The file system is a vital component of an operating system


responsible for managing files and directories. It provides a hierarchical structure
for organizing data, allowing users and applications to store, retrieve, and
manipulate files efficiently.

2. File Attributes: Each file in a file system is associated with various attributes,
including:
● Name: The unique identifier of the file.
● Size: The size of the file in bytes.
● Type: The type of file (e.g., text file, executable file, directory).
● Location: The physical location of the file on the storage device.
● Permissions: Access control information specifying who can read, write, or
execute the file.
● Timestamps: Information about when the file was created, modified, or
accessed.

3. File Operations: Operating systems provide a set of operations for managing


files, including:
● Create: Creating new files or directories.
● Read: Reading data from files.
● Write: Writing data to files.
● Delete: Deleting files or directories.
● Rename: Renaming files or directories.
● Copy: Creating a duplicate of a file or directory.
● Move: Moving files or directories to different locations.
Code:

import os
import tkinter as tk
from tkinter import filedialog, messagebox, simpledialog

class FileExplorer(tk.Tk):
def __init__(self):
super().__init__()
self.title("File Explorer")
self.geometry("800x600")
self.current_directory = os.getcwd()
self.setup_ui()
self.load_directory(self.current_directory)

def setup_ui(self):
self.current_dir_frame = tk.Frame(self)
self.current_dir_frame.pack(pady=10, fill=tk.X)
self.current_dir_label = tk.Label(self.current_dir_frame,
text=self.current_directory, font=("Arial", 12))
self.current_dir_label.pack(side=tk.LEFT, fill=tk.X, expand=True)
self.scrollbar_dir = tk.Scrollbar(self.current_dir_frame,
orient=tk.HORIZONTAL, command=self._on_scroll_dir)
self.scrollbar_dir.pack(side=tk.BOTTOM, fill=tk.X)

self.file_list_frame = tk.Frame(self)
self.file_list_frame.pack(expand=True, fill=tk.BOTH)
self.file_listbox = tk.Listbox(self.file_list_frame, width=70, height=20,
font=("Arial", 12))
self.file_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.scrollbar = tk.Scrollbar(self.file_list_frame, orient=tk.VERTICAL,
command=self.file_listbox.yview)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.file_listbox.config(yscrollcommand=self.scrollbar.set)
self.file_listbox.bind("<Double-Button-1>", self.open_selected_item)

self.button_frame = tk.Frame(self)
self.button_frame.pack(pady=10)
button_width = 12
button_padx = 10
button_pady = 5

self.create_folder_btn = tk.Button(self.button_frame, text="Create Folder",


command=self.ask_create_folder, width=button_width)
self.create_folder_btn.pack(side=tk.LEFT, padx=button_padx,
pady=button_pady)
self.create_file_btn = tk.Button(self.button_frame, text="Create File",
command=self.ask_create_file, width=button_width)
self.create_file_btn.pack(side=tk.LEFT, padx=button_padx, pady=button_pady)
self.rename_btn = tk.Button(self.button_frame, text="Rename",
command=self.ask_rename, width=button_width)
self.rename_btn.pack(side=tk.LEFT, padx=button_padx, pady=button_pady)
self.open_folder_btn = tk.Button(self.button_frame, text="Open Folder",
command=self.open_selected_item, width=button_width)
self.open_folder_btn.pack(side=tk.LEFT, padx=button_padx, pady=button_pady)
self.delete_btn = tk.Button(self.button_frame, text="Delete",
command=self.delete_file, width=button_width)
self.delete_btn.pack(side=tk.LEFT, padx=button_padx, pady=button_pady)

self.back_btn = tk.Button(self.button_frame, text="Back",


command=self.go_back, width=button_width)
self.back_btn.pack(side=tk.LEFT, padx=button_padx, pady=button_pady)

def load_directory(self, directory):


self.current_directory = directory
self.current_dir_label.config(text=self.current_directory)
self.file_listbox.delete(0, tk.END)

items = os.listdir(directory)
folders = [item for item in items if os.path.isdir(os.path.join(directory,
item))]
files = [item for item in items if os.path.isfile(os.path.join(directory,
item))]

folders.sort()
files.sort()

for folder in folders:


self.file_listbox.insert(tk.END, folder + " [Folder]")
for file in files:
self.file_listbox.insert(tk.END, file)

def ask_create_folder(self):
new_folder_name = simpledialog.askstring("Create Folder", "Enter folder
name:")
if new_folder_name:
new_folder_path = os.path.join(self.current_directory, new_folder_name)
os.makedirs(new_folder_path, exist_ok=True)
self.load_directory(self.current_directory)

def ask_create_file(self):
new_file_name = simpledialog.askstring("Create File", "Enter file name:")
if new_file_name:
new_file_path = os.path.join(self.current_directory, new_file_name)
with open(new_file_path, 'w') as new_file:
pass
self.load_directory(self.current_directory)

def ask_rename(self):
selected_item = self.file_listbox.get(tk.ACTIVE)
if selected_item and "[Folder]" not in selected_item:
new_name = simpledialog.askstring("Rename", f"Enter new name for
{selected_item}:")
if new_name:
old_path = os.path.join(self.current_directory, selected_item)
new_path = os.path.join(self.current_directory, new_name)
os.rename(old_path, new_path)
self.load_directory(self.current_directory)

def open_selected_item(self, event=None):


selected_item = self.file_listbox.get(tk.ACTIVE)
selected_item_path = os.path.join(self.current_directory,
selected_item.split(" [Folder]")[0])
if selected_item and os.path.isdir(selected_item_path):
self.load_directory(selected_item_path)

def delete_file(self):
selected_item = self.file_listbox.get(tk.ACTIVE)
if selected_item:
confirmation = messagebox.askyesno("Confirm Deletion", f"Are you sure
you want to delete {selected_item}?")
if confirmation:
file_path = os.path.join(self.current_directory,
selected_item.split(" [Folder]")[0])
if os.path.isdir(file_path):
os.rmdir(file_path)
else:
os.remove(file_path)
self.load_directory(self.current_directory)

def go_back(self):
parent_dir = os.path.abspath(os.path.join(self.current_directory,
os.pardir))
self.load_directory(parent_dir)

def _on_scroll_dir(self, *args):


self.current_dir_label.xview(*args)

if __name__ == "__main__":
app = FileExplorer()
app.mainloop()

Output:
Conclusion: Thus we have successfully created and implemented a GUI that can
perform CRUD operations on File/Folders in a File System.

You might also like