Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

what is wrong with my coding: import os def main ( ) : while True: user _ input = input ( Enter command:

what is wrong with my coding: import os
def main():
while True:
user_input = input("Enter command: ").split()
if not user_input:
continue
command = user_input[0].upper()
if command =='Q':
print("Exiting the program.")
break
elif command =='L':
if len(user_input)<2:
print("Error: Please provide a directory path after 'L'.")
continue
directory = user_input[1]
options = user_input[2:]
directory_contents(directory, options)
else:
print("Error: Invalid command. Use 'L' to list contents or 'Q' to quit.")
def directory_contents(directory, options):
try:
files_and_dir = os.listdir(directory)
if '-f' in options:
files_and_dir =[item for item in files_and_dir if os.path.isfile(os.path.join(directory, item))]
if '-s' in options:
search_term = options[options.index('-s')+1]
files_and_dir =[item for item in files_and_dir if search_term in item]
if '-e' in options:
extension = options[options.index('-e')+1]
files_and_dir =[item for item in files_and_dir if item.endswith('.'+ extension)]
if '-r' in options:
for item in files_and_dir:
print(os.path.join(directory, item))
if os.path.isdir(os.path.join(directory, item)):
directory_contents(os.path.join(directory, item), options)
else:
for item in files_and_dir:
print(os.path.join(directory, item))
except FileNotFoundError:
print(f"Error: Directory '{directory}' not found.")
if __name__=="__main__":
main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions