PROGRAM 7
PROGRAM 7
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
DIRECTORY OPERATIONS
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.")
import os
def create_directory():
try:
os.mkdir(dirname)
print("Directory created\n")
except FileExistsError:
def create_file():
try:
f.write("")
print("File created\n")
except FileNotFoundError:
def delete_file():
try:
os.remove(os.path.join(dirname, filename))
except FileNotFoundError:
except OSError:
def search_file():
if filename in files:
print(f"{os.path.join(root, filename)}")
return
def display():
level = root.count(os.sep)
print(f"{indent}{os.path.basename(root)}")
for f in files:
print(f"{subindent}{f}")
print()
while True:
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
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)