File database with search

File database with search

DocGenie.py 

importos

import pickle

tracked_folders = []

# loads all tracked folders from pickle file

pickle_file_path = os.path.join(os.path.dirname(__file__), ‘pickle_file.pkl’)

pickle_file = open(pickle_file_path, ‘rb’)

tracked_folders = pickle.load(pickle_file)

pickle_file.close()

def help():

help = “This is a list of all the commands available:” \

“- add_folder_to_track(file_path): Add a folder to track” \

“- get_tracked_folders(): Display all the tracked folders” \

“- ”

defadd_folder_to_track(file_path):

# checks if a valid filename

if (os.path.exists(file_path)):

# checks if folder and not a file

ifos.path.isdir(file_path):

# adds folder to be tracked & dumps folder to pickle file

tracked_folders.append(file_path)

pickle_file = open(pickle_file_path, ‘wb’)

pickle.dump(tracked_folders, pickle_file)

pickle_file.close()

print(“Folder {} now tracked”.format(os.path.basename(file_path)))

else:

print(“File {} is not a folder.”.format(os.path.basename(file_path)))

else:

print(“Not a valid filepath”)

defget_tracked_folders():

returntracked_folders 

SearchAndDisplay.py 

“””

SearchAndDisplay.py

This file should implement all the method that will be used

for the search part of the program. It should contain

‘searchByName’, ‘searchByTag’ and ‘search’ methods. Each

of these methods should take a path and a query as parameters

“””

fromos import walk

# This method will perform a search in the path (parameter)

# and retrieve all the files that match the query in a list.

#

# The search will check ALL the files and ALL the directories

# in the path parameter.

#

defsearchByName(word, path):

relevantFiles = []

files = []

dir = []

for (dirpath, dirnames, filenames) in walk(path):

files.extend(filenames)

dir.extend(dirnames)

break

#print(dirpath)

#print(files)

#print(dir)

for file in files:

ifword.lower() in file.lower():

relevantFiles.append(file)

for directory in dir:

relevantFiles.extend(searchByName(word, path + “\\” + directory))   # Recursive

returnrelevantFiles

#################################################################################

### test(s)

#################################################################################

folders = []    # Supposed to be the list of folders to track

folders.append(“M:\CE111”)

for folder in folders:

print(searchByName(“file”, folder)) 

Solution

importos

import pickle

tracked_folders = {}

# loads all tracked folders from pickle file

pickle_file_path = os.path.join(os.path.dirname(__file__), ‘pickle_file.pkl’)

ifos.path.exists(pickle_file_path):

pickle_file = open(pickle_file_path, ‘rb’)

tracked_folders = pickle.load(pickle_file)

pickle_file.close()

def help():

help = “This is a list of all the commands available:” \

“- add_folder_to_track(file_path): Add a folder to track” \

“- get_tracked_folders(): Display all the tracked folders” \

“- ”

defadd_folder_to_track(file_path):

# checks if a valid filename

ifos.path.exists(file_path):

# checks if folder and not a file

ifos.path.isdir(file_path):

# adds folder to be tracked & dumps folder to pickle file

tracked_folders[file_path] = os.listdir(file_path)

pickle_file = open(pickle_file_path, ‘wb’)

pickle.dump(tracked_folders, pickle_file)

pickle_file.close()

print(“Folder {} now tracked”.format(os.path.basename(file_path)))

else:

print(“File {} is not a folder.”.format(os.path.basename(file_path)))

else:

print(“Not a valid filepath”)

defget_tracked_folders():

returntracked_folders