Question: Week Two Assignment 4 - File Processing Object Complete the script below to do the following: 1) Add your name, date, assignment number to the

Week Two Assignment 4 - File Processing Object 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 required attributes: filePath, fileSize, md5Hash, lastModifiedTime, fileHeader, status, errorInfo Note: FileHeader is the first 20 bytes of the file in Hex (hint use hexlify library function) b) Create a GetFileDetails Method which will i) Return the details of the file (FilePath, FileSize, md5Hash, LastModifiedTime, FileHeader, status, errorInfo) 3) Demonstrate the use of the new class a) prompt the user for a directory path b) using the os.walk() method to extract each filename (note you will need to create the fullpath for each file) c) Loop through each filename and instantiate an object using the FileProcessor Class d) Retrieve the FileDetails for each file using the GetFileDetails method you have created e) Populate a PrettyTable with information about eachfile processed f) Print your PrettyTable g) Save and submit your PrettyTable as a csv file, along with your final python script.

This is the code I have been able to produce. I can get the code to run through everything and print a PrettyTable, but I cannot get anything to print into the table itself.

''' Import Standard Libraries '''

import os

from binascii import hexlify

from time import ctime

''' Import 3rd Party Libraries'''

from hashing import HashFile # Note HashFile.py need to be in the same folder as your script

from prettytable import PrettyTable

tbl = PrettyTable(['Path', 'FileSize', 'Hash', 'LastModified', 'FileHeader', 'Status', 'Error'])

class FileProcessor:

''' Class to Create User Dictionary'''

def __init__(self, fileName):

''' initialize instance attributes'''

''' Create object variables and Constants '''

if os.path.isfile(fileName):

self.filePath = fileName

# You will add code here to extract the following information

self.lastModifiedTime = "lastModifiedTime"

self.fileSize = "fileSize"

self.md5Hash = "md5Hash"

self.fileHeader = "fileHeader"

self.status = "status"

self.errorInfo = "errorInfo"

def GetFileMetaData(self):

# You will add code here to retrun the required values

# think about the container type you might return with the required values

stats = os.stat(fileName)

self.filePath = os.path.abspath()

self.lastModifiedTime = stats.st_mtime

self.fileSize = stats.st_size

self.md5Hash = HashFile(fileName)

self.fileHeader = hexlify(fileName)

self.status = os.stat_result

self.errorInfo = os.error

tbl.add_row([])

def GetFileHeader(self):

with open(fileName, 'rb') as binFile:

header = binFile.read(20)

self.fileHeader = hexlify(header)

tbl.add_rows( [self.fileHeader] )

def main():

print(" Assignment-4 Franklin Stevens ")

dirPath = input("Enter a valid directory path: ")

''' Process all the files in the path '''

for root, dirs, files in os.walk(dirPath):

for fileName in files:

path = os.path.join(root, fileName)

fullPath = os.path.abspath(path)

file_processor = FileProcessor(fullPath)

if __name__ == '__main__':

main()

resultString = tbl.get_string()

print(resultString)

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 Databases Questions!