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
Security and Error Handling 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: python sysadmin.py user createuser "invalid username" Expected Output: ERROR Invalid username format. Usernames should not contain spaces. INFO Error logged to errorloglog
Code Documentation and ReadMe Code Comments: Comment each function and describe parameters and functionality. README File: Overview, setup, usage instructions, and any dependencies
to the following code :
#usrbinpython import os import csv import logging import argparse as ap import subprocess import pwd #exception for invalid user entry class invaliduserentryException: pass class DirPathNotValidException: pass #create logger for question logger logginggetLoggerGrouplogger # set up for console consolehandler loggerStreamHandler #setup logging levels consolehandler.setLevelloggingWARNING filehandler.setLevelloggingDEBUG #setup the formatting for the log formatter loggingFormatterasctimes names levelnames messages concolehandler.setFormatterformatter file.handler.setFormatterformatter loggeraddHandlerconsolehandler loggeraddHandlerfilehandler #create organize dir function for questions def organizedirectorydirectory: try: if ospath.existsdirectory: #check if dir path exists for file in oslistdirdirectory:#loop through files in dir filepath ospath.joindirectory file#merge the two paths together if ospath.isdirospath.joindirectory file:#check if is dir or is file printthe file is file filename file fileextension ospath.splitextfilename#this should get the back end of the file extensions. if fileextension txt:# this code take the file extension and turns it into a new dir. I repeated this a few times for different file extensions newdir ospath.joindirectory 'filetxt osmakedirsnewdir newfilepath ospath.joinnewdir, file osrenamefilepath, newfilepath printtxt files moved to new dir" if fileextension log: newdir ospath.joindirectory 'filelog osmakedirsnewdir newfilepath ospath.joinnewdir, file osrenamefilepath, newfilepath printlog files moved to new dir" if fileextension tar": newdir ospath.joindirectory 'filetar' osmakedirsnewdir newfilepath ospath.joinnewdir, file osrenamefilepath, newfilepath printtar files moved to new dir" if fileextension png: newdir ospath.joindirectory 'filepng osmakedirsnewdir newfilepath ospath.joinnewdir, file osrenamefilepath, newfilepath printpng files moved to new dir" if fileextension csv: newdir ospath.joindirectory 'filecsv osmakedirsnewdir newfilepath ospath.joinnewdir, file osrenamefilepath, newfilepath printcsv files moved to new dir" if fileextension docx": newdir ospath.joindirectory 'filedocx' osmakedirsnewdir newfilepath ospath.joinnewdir, file osrenamefilepath, newfilepath printdocx files moved to new dir" if fileextension jpeg": newdir ospath.joindirectory 'filejpeg' osmakedirsnewdir newfilepath ospath.joinnewdir, file osrenamefilepath, newfilepath printjpeg 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 fileextension "miscfiles" newdir ospath.joindirectory 'miscfiles'
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
