-2

I am trying to create a block of code that scans a .txt file (first book of LOTR) and tallys the frequency of each letter into a dictionary. (say for instance if theres 500 3 letter words appearing in the book, the output will be displayed as 3:500, and so on).

I seemed to have gotten the code right as I have tried it on test documents before adding the dict() function, and it works by printing each word as a string in a list, though when I go to run it now, it prints the first word then outputs file not found (which is what ive coded for an exception), even though the file is present in my jupyter notebook.

Is there any way to fix this? what is the default directory that jupyter scans for? All of your help is appreciated!

Code:

fname= input('Enter file: ')
#if len(fname) < 1: fname = 'LOTR.txt'
try:
    fhand = open(fname)
    d = dict()
    for line in fhand:
        words = line.split()
        print(words)
        for word in words:
            d1[word] = d1.get(word, 0) + 1
    print (d1)
except:
    print("File not found")

output:

Enter file: LOTR.txt
['PROLOGUE']
File not found
  • 3
    [Don't use a bare except](https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except), you might be hiding a different error. – SuperStormer May 21 '21 at 12:41
  • 3
    You're probably getting a `NameError` from `d1` – rdas May 21 '21 at 12:42
  • 1
    Your error message suggests you are anticipating a `FileNotFoundError`, so catch that specifically. Don't catch exceptions you don't know how to handle. – chepner May 21 '21 at 12:43
  • When posting this kind of questions to StackOverflow include the Stacktrace. If you are catching the error, catch it in a variabel and print it and the stacktrace – Patrick Artner May 21 '21 at 12:45
  • try running the script without the try except block it'll help you find errorrs, only use try except when you are absolutely sure what the exception is and remember this never ever use bare except in your scripts. – Aakash Singh May 21 '21 at 12:46

1 Answers1

1

Your problem is that you are accessing d1 before defining it.

Also you should catch specific exceptions instead of using a bare except. The following should solve your problem:

fname= input('Enter file: ')
#if len(fname) < 1: fname = 'LOTR.txt'
try:
    fhand = open(fname)
    d = dict()
    for line in fhand:
        words = line.split()
        print(words)
        for word in words:
            d[word] = d.get(word, 0) + 1
    print(d)
except FileNotFoundError:
    print("File not found")
except Exception as e:
    print("Other error occurred", e)
Matteo Zanoni
  • 3,429
  • 9
  • 27
  • Don't add `except Exception` at all, it isn't doing anything useful and is more harmful than just letting the error throw a traceback. – SuperStormer May 21 '21 at 12:49
  • 1
    It is useful if this code lives inside a bigger or more complex project or if in the future you want to add some sort of logging. Otherwise yes it is unusefull – Matteo Zanoni May 21 '21 at 12:51