Question: Python Complete the script below to do the following: 1) Add your name, date, assignment number to the top of this script 2) Create a
Python
Complete the script below to do the following: 1) Add your name, date, assignment number to the top of this script 2) Create a class named FileProcessor a) The Init method shall: i) verify the file exists ii) Extract key file system metadata from the file and store them as instance attribute i.e. FilePath, FileSize, MAC Times, Owner, Mode etc. b) Create a GetFileHeader Method which will i) Extract the first 20 bytes of the header and store them in an instance attribute c) Create a PrintFileDetails Method which will i) Print the metadata ii) Print the hex representation of the header 3) Demonstrate the use of the new class a) prompt the user for a directory path b) using the os.listdir() method extract the filenames from the directory path c) Loop through each filename and instantiate and object using the FileProcessor Class d) Using the object i) invoke the GetFileHeader Method ii) invoke the PrintFileDetails Method
Below is what I have so far am I missing something or doing something wrong
import os import time import hashlib import sys
class FileProcessor: existence = False file_name = None ten_Byte_header = None real_path = None file_size = None owner = None modify_time = None access_time = None creation_time = None
def __init__(self, file_name): existence = os.path.exists(file_name) if existence == False: print("File does not exist") else: self.file_name = file_name self.existence = True self.real_path = os.path.realpath(file_name) self.file_size = os.path.getsize(file_name) self.owner = getpwuid(os.stat(file_name).st_uid).pw_name self.modify_time = time.ctime(os.path.getmtime(file_name)) self.access_time = time.ctime(os.path.getatime(file_name)) self.creation_time = time.ctime(os.path.getctime(file_name))
def extract_20byte_header(self): if self.existence == False: print("File does not exist") else: with open(self.file_name, "rb") as f: self.twenty_Byte_header = f.read(20)
def print_meta_data(self): if self.existence == True: print(f"Path of the file is: {self.real_path}") print(f"Size of the file is: {self.file_size} Bytes") print(f"Owner: {self.owner}") print(f"Last modified: {self.modify_time}") print(f"Last access on: {self.access_time}") print(f"Created on: {self.creation_time}") self.extract_20byte_header() print(f"Header in HEX is: {self.twenty_Byte_header.hex()}") else: print("No file exist") def main(): directory = input("Enter a directory: ") dir_list = os.listdir(directory)
for item in dir_list: new_path = directory + '/' + item if os.path.isfile(new_path): afObject = FileProcessor(new_path) print() afObject.print_meta_data() main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
