0% found this document useful (0 votes)
3 views

PROGRAM 7

The document outlines various file organization techniques including single-level, two-level, and hierarchical directory structures. It discusses the attributes and operations of files and directories, along with Python code implementations for each directory type. Each structure has its advantages and disadvantages, with hierarchical directories offering more organization and efficiency at the cost of complexity.

Uploaded by

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

PROGRAM 7

The document outlines various file organization techniques including single-level, two-level, and hierarchical directory structures. It discusses the attributes and operations of files and directories, along with Python code implementations for each directory type. Each structure has its advantages and disadvantages, with hierarchical directories offering more organization and efficiency at the cost of complexity.

Uploaded by

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

PROGRAM 7

Simulate all File Organization Techniques


a) Single level directory b) Two level
c) Hierarchical

INTRODUCTION
❖ File is collection of related information
❖ Directory structure organizes files
❖ File is named collection of similar records
❖ It referred by its name
❖ Info is defined by creator
❖ Different types of information is stored in a file
❖ File can be (exec,data,image)
❖ It has unique name
❖ Text file= sequence of characters
❖ Source file= sequence of subroutines

FILE ATTRIBUTES

❖ File name is string of characters


❖ System support both lower & upper case letters
❖ Some system differentiate
❖ File name =human readable form
FILE OPERATIONS

DIRECTORY OPERATIONS

❖ Search for a file


❖ Create a file: New files need to be created and added to the
directory.
❖ Delete a file: When a file is no longer needed, we want to be able to
remove it from the directory.
❖ List a directory: We need to be able to list the files in a directory
❖ Rename a file: change the name of the file
❖ Traverse the file system: We may wish to access every directory
and every file within a directory structure
SINGLE LEVEL DIRECTORY

❖ The simplest directory structure is the single-level directory.


❖ All files are contained in the same directory
❖ When the number of files increases or when the system has more
than one user.
❖ All files are in the same directory, they must have unique names.
❖ Keeping track of so many files is a daunting task.
❖ Advantages: efficiency
❖ Disadvantages:
➢ Naming- collisions
➢ grouping - not possible
PYTHON CODE IMPLEMENTATION FOR SINGLE LEVEL
DIRECTORY
class Directory:
def __init__(self, name):
self.name = name
self.files = []

def create_file(self, file_name):


if file_name in self.files:
print(f"File {file_name} already exists.")
else:
self.files.append(file_name)
print(f"File {file_name} created successfully.")

def delete_file(self, file_name):


if file_name in self.files:
self.files.remove(file_name)
print(f"File {file_name} is deleted.")
else:
print(f"File {file_name} not found.")

def search_file(self, file_name):


if file_name in self.files:
print(f"File {file_name} found.")
else:
print(f"File {file_name} not found.")

def display_files(self):
print(f"The Files are -- {', '.join(self.files)}")

def main(self):
while True:
print(f"\n1. Create File 2. Delete File 3. Search File 4. Display Files
5. Exit")
choice = input("Enter your choice - ")

if choice == '1':
file_name = input("Enter the name of the file -- ")
self.create_file(file_name)
elif choice == '2':
file_name = input("Enter the name of the file -- ")
self.delete_file(file_name)
elif choice == '3':
file_name = input("Enter the name of the file - ")
self.search_file(file_name)
elif choice == '4':
self.display_files()
elif choice == '5':
print("Exiting...")
break
else:
print("Invalid choice. Please choose a valid option.")

directory_name = input("Enter name of directory -- ")


directory = Directory(directory_name)
directory.main()
OUTPUT

TWO LEVEL DIRECTORY


❖ Single-level directory often leads to confusion of file names among
different users.
❖ The standard solution is to create a separate directory for each
user.
❖ In the two-level directory structure, each user has his own user file
directory .
❖ The UFDs have similar structures, but each lists only the files of a
single user.
❖ When a user job starts or a user logs in, the system's master file

directory (MFD) is searched.

❖ Advantages: efficient searching


❖ Disadvantages:
➢ Naming- same file name in different directory, path name.
PYTHON CODE IMPLEMENTATION FOR TWO LEVEL
DIRECTORY

import os

def create_directory():

dirname = input("Enter name of directory -- ")

try:

os.mkdir(dirname)

print("Directory created\n")

except FileExistsError:

print("Directory already exists\n")

def create_file():

dirname = input("Enter name of the directory – ")

filename = input("Enter name of the file -- ")

try:

with open(os.path.join(dirname, filename), 'w') as f:

f.write("")

print("File created\n")
except FileNotFoundError:

print("Directory not found\n")

def delete_file():

dirname = input("Enter name of the directory – ")

filename = input("Enter name of the file -- ")

try:

os.remove(os.path.join(dirname, filename))

print(f"File {filename} is deleted\n")

except FileNotFoundError:

print("Directory not found\n")

except OSError:

print(f"File {filename} not found\n")

def search_file():

filename = input("Enter name of the file -- ")

for root, dirs, files in os.walk(".", topdown=False):

if filename in files:
print(f"{os.path.join(root, filename)}")

return

print(f"File {filename} not found\n")

def display():

for root, dirs, files in os.walk(".", topdown=True):

level = root.count(os.sep)

indent = " " * 4 * level

print(f"{indent}{os.path.basename(root)}")

subindent = " " * 4 * (level + 1)

for f in files:

print(f"{subindent}{f}")

print()

while True:

print("1. Create Directory 2. Create File 3. Delete File")

print("4. Search File 5. Display 6. Exit")

choice = int(input("Enter your choice -- "))

if choice == 1:
create_directory()

elif choice == 2:

create_file()

elif choice == 3:

delete_file()

elif choice == 4:

search_file()

elif choice == 5:

display()

elif choice == 6:

break

else:

print("Invalid choice\n")
OUTPUT
HIERARCHICAL DIRECTORY

❖ Generalization allows users to create their own subdirectories and


to organize their files accordingly.
❖ A tree is the most common directory structure.
❖ The tree has a root directory, and every file in the system has a
unique path name.
❖ Advantages:
➢ Efficient searching – current working directory
➢ Naming - same file name in a different directory
➢ Grouping capability
❖ Disadvantages:
➢ Structural complexity
❖ Absolute or relative path name
❖ Creating a new file is done in current directory
❖ Delete a file
❖ rm <file-name>
❖ Creating a new subdirectory is done in current directory
❖ mkdir <dir-name>
❖ Example: if in current directory /mail
❖ mkdir count
PYTHON CODE IMPLEMENTATION FOR HIERARCHICAL
DIRECTORY
class Directory:
def __init__(self, name, parent=None):
self.name = name
self.parent = parent
self.children = []

def create_subdirectory(self, name):


subdirectory = Directory(name, parent=self)
self.children.append(subdirectory)
return subdirectory
def create_file(self, name):
file = File(name, parent=self)
self.children.append(file)
return file

class File:
def __init__(self, name, parent=None):
self.name = name
self.parent = parent

def get_directory_path(directory):
path_components = []
while directory:
path_components.insert(0, directory.name)
directory = directory.parent
return '/'.join(path_components)

def create_directory(root_directory):
name = input("Enter Name of dir/file (under {}):
".format(root_directory.name))
choice = int(input("Enter 1 for Dir / 2 For File : "))
if choice == 1:
subdirectory = root_directory.create_subdirectory(name)
num_children = int(input("No of subdirectories / files (for
{}) :".format(subdirectory.name)))
for i in range(num_children):
create_directory(subdirectory)
elif choice == 2:
root_directory.create_file(name)

def display_directory(directory, indent=0):


print('{}{}'.format(' '*indent, directory.name))
for child in directory.children:
if isinstance(child, Directory):
display_directory(child, indent=indent+1)
else:
print('{}{}'.format(' '*(indent+1), child.name))

# Create root directory


root_directory = Directory('ROOT')

# Create directories and files


create_directory(root_directory)

# Display directory hierarchy


display_directory(root_directory)
OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy