Order Now  +1 678 648 4277 

Forensic Code Assignment Solution in Python


Problem:

You have to modify the FindFilesNDir.py forensics code developed in class. The program should ask the user for the name of the starting directory. The program gets the names of all files and directories and prints them to separate files.

The program should work irrespective of the OS it is run on – that is – do not use any OS-dependent file path symbols. Make sure you test the program from a starting directory that has many directories and file names to ensure the program is running correctly.

Solution:

import os def write_list_to_file(items, filename): f = open(filename, 'w') if f.closed: print('Can not open output file: ', filename) return for item in items: f.write(item + '\n') f.close() init_dir = input('Please, enter directory to start: ') all_files = [] all_dirs = [] dirs_to_process = [] start_dir = init_dir while True: itemsInCurrentDir = os.listdir(start_dir) while len(itemsInCurrentDir) > 0: # pop and save into an item # get us the entire path and filename/dir name item = os.path.join(start_dir, itemsInCurrentDir.pop()) if os.path.isfile(item): all_files.append(item) elif os.path.isdir(item): all_dirs.append(item) dirs_to_process.append(item) # keep looping until yet to process gets empty # if empty, we are done if len(dirs_to_process) > 0: start_dir = dirs_to_process.pop() else: break #End of outer loop print('Total number of files:', len(all_files)) print('Total number of directories:', len(all_dirs)) all_dirs.sort(key= lambda x: x.lower()) all_dirs.insert(0, init_dir) write_list_to_file(all_dirs, 'ErvinAllDirs.txt') all_files.sort(key= lambda x: x.lower()) all_files.insert(0, init_dir) write_list_to_file(all_files, 'ErvinAllFiles.txt')