Trying to find URL's in a file and save to a dictionary?

Viewed 37

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)
1 Answers

The code does not make sense. Not even sure why the while loop is there.

for eachURL in urlMatches:
    while True:
        matches = matches[eachURL]

You'll need to do something like

for URL in urlMatches:
    if URL not in matches:
        matches[URL] = 1
    else:
        matches[URL] += 1

Now that you have edited the question, new issues arise. The following should work:

fileContents = targetFile.read(chunkSize)
urlMatches   = urlPattern.findall(fileContents)

for URL in urlMatches:
    if URL not in matches:
        matches[URL] = 1
    else:
        matches[URL] += 1

Note that you'll need an outer loop to cover the file contents after chunk size.

Related