unix
unix
:
AIM:
PROGRAM:
import os
def list_files():
path = input("Enter directory path for ls: ")
if os.path.isdir(path):
print("\n".join(os.listdir(path)))
else:
print("Invalid directory path")
def print_working_directory():
print(f"Current working directory: {os.getcwd()}")
def read_file():
file_name = input("Enter file name for cat: ")
if os.path.isfile(file_name):
with open(file_name, 'r') as file:
print(file.read())
else:
print("Invalid file path")
def main():
while True:
print("\n1. List files (ls)")
print("2. Print working directory (pwd)")
print("3. Read file (cat)")
print("4. Exit")
choice = input("Enter your choice: ")
if choice == '1':
list_files()
elif choice == '2':
print_working_directory()
elif choice == '3':
read_file()
elif choice == '4':
if __name__ == "__main__":
main()
SAMPLE OUTPUT:
1. List files (ls)
2. Print working directory (pwd)
3. Read file (cat)
4. Exit
Enter your choice: 1
Enter directory path for ls: /home/user/documents
file1.txt
file2.txt
file3.pdf
AIM:
To WriteprogramsusingthefollowingUNIXoperatingsystemcalls fork, exec, getpid, exit, wait, close, stat,
opendir and readdir
PROGRAM:
import os
import sys
def create_process():
pid = os.fork()
if pid == 0:
print(f"Child process: {os.getpid()}")
os.execvp('ls', ['ls', '-l'])
else:
print(f"Parent process: {os.getpid()}")
os.wait()
def get_file_stats(file_path):
try:
stat_info = os.stat(file_path)
print(f"File Size: {stat_info.st_size} bytes")
print(f"Last Modified: {stat_info.st_mtime}")
except FileNotFoundError:
print("File not found")
def read_directory(dir_path):
try:
dir = os.opendir(dir_path)
for entry in os.listdir(dir_path):
print(entry)
except FileNotFoundError:
print("Directory not found")
def main():
user_choice = input("Enter 1 to create process, 2 to get file stats, 3 to read directory: ")
if user_choice == '1':
PROGRAM:
import os
import shutil
def simulate_ls(path):
try:
files = os.listdir(path)
for file in files:
print(file)
except FileNotFoundError:
print("Directory not found.")
def main():
while True:
if __name__ == "__main__":
main()
SAMPLE OUTPUT:
Enter a command (ls, cp, grep, exit): ls
Enter the directory path: /home/user/pictures
Directory not found.