Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from os import getcwd, listdir, path
- from ntpath import basename
- from re import search, IGNORECASE
- from csv import writer, QUOTE_MINIMAL
- KEYWORDS = ["fang", "reed", "trish", "stella", "rosa"]
- def makeDB(keys):
- db = {}
- for key in keys:
- db[key] = []
- return db
- def populateDB(db, filePaths):
- for filePath in filePaths:
- keys = parseFile(filePath, db.keys())
- for key in keys:
- db[key].append(filePath)
- return db
- def getFilePaths():
- dirPath = getcwd()
- dirContent = listdir(".")
- filePaths = []
- for item in dirContent:
- itemPath = path.join(dirPath, item)
- if path.isdir(itemPath):
- itemContent = listdir(itemPath)
- for subitem in itemContent:
- subitemPath = path.join(itemPath, subitem)
- if path.isfile(subitemPath):
- filePaths.append(subitemPath)
- return filePaths
- def parseFile(filePath, keys):
- file = open(filePath, mode="r", encoding="utf8")
- content = file.read()
- foundKeys = []
- for key in keys:
- if search(key, content, IGNORECASE):
- foundKeys.append(key)
- return foundKeys
- def generateCSV(db, filePaths):
- keys = db.keys()
- cols = list(keys)
- cols.insert(0, "title")
- with open("ficDB.csv", mode='w', encoding="utf8", newline='') as csvFile:
- csvWriter = writer(csvFile, delimiter=',', quotechar='"', quoting=QUOTE_MINIMAL)
- csvWriter.writerow(cols)
- for filePath in filePaths:
- row = [basename(filePath)]
- for key in keys:
- if filePath in db[key]:
- row.append('X')
- else:
- row.append('')
- csvWriter.writerow(row)
- filePaths = getFilePaths()
- db = makeDB(KEYWORDS)
- db = populateDB(db, filePaths)
- generateCSV(db, filePaths)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement