Question: I need to add these steps 5 . Security and Error Handling ( 5 % ) Error Logging: Centralized error logging with timestamps. Security Validation

I need to add these steps
5. Security and Error Handling (5%) Error Logging: Centralized error logging with timestamps. Security Validation and Input Sanitization: Check for valid inputs and safe handling of sensitive information. Example Usage Command: python3 sys_admin.py user --create-user "invalid username" Expected Output: [ERROR] Invalid username format. Usernames should not contain spaces. [INFO] Error logged to error_log.log.
6. Code Documentation and ReadMe (5%) Code Comments: Comment each function and describe parameters and functionality. README File: Overview, setup, usage instructions, and any dependencies
to the following code :
#!/usr/bin/python3 import os import csv import logging import argparse as ap import subprocess import pwd #exception for invalid user entry class invalid_user_entry(Exception): pass class DirPathNotValid(Exception): pass #create logger for question 3 logger = logging.getLogger("Group2_logger") # set up for console console_handler = logger.StreamHandler() #setup logging levels console_handler.setLevel(logging.WARNING) file_handler.setLevel(logging.DEBUG) #setup the formatting for the log formatter = logging.Formatter('%(asctime)s -%(name)s -%(levelname)s -%(message)s') concole_handler.setFormatter(formatter) file.handler.setFormatter(formatter) logger.addHandler(console_handler) logger.addHandler(file_handler) #create organize dir function for questions 3 def organize_directory(directory): try: if os.path.exists(directory): #check if dir path exists for file in os.listdir(directory):#loop through files in dir file_path = os.path.join(directory, file)#merge the two paths together if os.path.isdir(os.path.join(directory, file)):#check if is dir or is file print("the file is", file) filename = file file_extension = os.path.splitext(filename[-1])#this should get the back end of the file extensions. if file_extension ==".txt":# this code take the file extension and turns it into a new dir. I repeated this a few times for different file extensions new_dir = os.path.join(directory, 'file_.txt') os.makedirs(new_dir) new_file_path = os.path.join(new_dir, file) os.rename(file_path, new_file_path) print(".txt files moved to new dir") if file_extension ==".log": new_dir = os.path.join(directory, 'file_.log') os.makedirs(new_dir) new_file_path = os.path.join(new_dir, file) os.rename(file_path, new_file_path) print(".log files moved to new dir") if file_extension ==".tar": new_dir = os.path.join(directory, 'file_.tar') os.makedirs(new_dir) new_file_path = os.path.join(new_dir, file) os.rename(file_path, new_file_path) print(".tar files moved to new dir") if file_extension ==".png": new_dir = os.path.join(directory, 'file_.png') os.makedirs(new_dir) new_file_path = os.path.join(new_dir, file) os.rename(file_path, new_file_path) print(".png files moved to new dir") if file_extension ==".csv": new_dir = os.path.join(directory, 'file_.csv') os.makedirs(new_dir) new_file_path = os.path.join(new_dir, file) os.rename(file_path, new_file_path) print(".csv files moved to new dir") if file_extension ==".docx": new_dir = os.path.join(directory, 'file_.docx') os.makedirs(new_dir) new_file_path = os.path.join(new_dir, file) os.rename(file_path, new_file_path) print(".docx files moved to new dir") if file_extension ==".jpeg": new_dir = os.path.join(directory, 'file_.jpeg') os.makedirs(new_dir) new_file_path = os.path.join(new_dir, file) os.rename(file_path, new_file_path) print(".jpeg files moved to new dir") else:# this line of code catches anything that I have not named as a file extension and puts it into a new dir called misc. fiels file_extension = "misc_files" new_dir = os.path.join(directory, 'misc_files')

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!