For my coding assignment I am to find all URL's in a mem.raw file and then save the URL's and how many of each was found in a prettytable and output it. The code that seems to be the issue if the "for URL in fileContents" but I can't figure out why. It's iterating over this for loop but isn't finding the matches and saving it to the dictionary. Any ideas why?
Here is the code I have
import re
import os
import sys
from prettytable import PrettyTable
largeFile = input("Enter the name of a large File: ")
chunkSize = int(input("What size chunks? "))
urlPattern = re.compile(b'\w+:\/\/[\w@][\w.:@]+\/?[\w\.?=%&=\-@/$,]*')
matches = {}
try:
if os.path.isfile(largeFile):
with open(largeFile, 'rb') as targetFile:
fileContents = targetFile.read(chunkSize)
print("\nURLs")
for URL in fileContents:
try:
urlMatches = urlPattern.findall(fileContents)
cnt = matches[URL]
cnt += 1
matches[URL] = cnt
except:
matches[URL] = 1
tbl = PrettyTable(["Words", "Occurrences"])
for word, cnt in matches.items():
tbl.add_row([word, cnt])
tbl.align = '1'
print(tbl.get_string(sortby="Occurrences", reversesort=True))
break
else:
print(largeFile, " is not a valid file")
sys.exit("Script Aborted")
except Exception as err:
print(err)