-3

I have this program working how it should be but there is 1 small problem that I'm having and I really don't know what to do about it. I feel like to fix the problem I have to reformat my entire program. In the very beginning of the while loop. I created a with statement that executes what the program is supposed to do. The program opens a file, reads it, and outputs how many unique words there are in a text file. The with statement works, but now the error checking past the with statement does not execute and I'm prompted with an error. When you input a file that does not exist it is supposed to prompt the user saying "The file (filename) does not exist!" But that code past the with statement is no longer executed and I'm prompted with a FileNotFoundError.

    def FileCheck(fn):
    try:
        open(fn, "r")
        return 1
    except IOError:
        print("The file " + filename + " was not found!")
        return 0

loop = 'y'
while loop == 'y':
    filename = input("Enter the name of the file you wish to process?: ")
    with open(filename, "r") as file:
        lines = file.read().splitlines()
        uniques = set()
        for line in lines:
            uniques = set(line.split())
            print("There are " + str(len(uniques)) + " unique words in " + filename + ".")
    if FileCheck(filename) == 1:
        loop = 'n'
    else:
        exit_or_continue = input("Enter the name of the file you wish to process or type exit to quit: ")
        if exit_or_continue == 'exit':
            print("Thanks for using the program!")
            loop = 'n'
        else:
            break

Here is the error message when I input a file that does not exist

Enter the name of the file you wish to process?: aonsd.txt
Traceback (most recent call last):
  File "C:/Users/C/PycharmProjects", line 21, in <module>
    with open(filename, "r") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'aonsd.txt'
Avenius
  • 21
  • 7
  • Welcome to StackOverflow. See [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). We cannot effectively help you until you post your MRE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. Indent your code properly, reduce it to the minimum, and include the entire error message (with traceback). – Prune Nov 06 '19 at 19:28
  • 1
    Additionally, please let your program print the error so we can pinpoint the origin of the problem. – Nicolas Gervais Nov 06 '19 at 19:28
  • Also keep your terminology clear: `with` is a block, not a loop -- nothing repeats due to the `with` statement. – Prune Nov 06 '19 at 19:29
  • @Prune My bad, I edited the code block to make it easier to copy and paste. I know that `with` isn't a loop I was just in a hurry and I didn't realize I said that they where loops. I also included the error message that is prompted when I input a file that does not exist. The code below the `with` statement takes care of the error when there is not a file that exists. It is no longer executed ever since I implemented the `with` statement. – Avenius Nov 06 '19 at 19:38
  • I am not sure what your question is, the FileNotFoundError was given on the `with` statement and because the file doesn't exist, the program stops without continuing to the FileCheck function. you should apply the try except before `with`, see [this post](https://stackoverflow.com/questions/713794/catching-an-exception-while-using-a-python-with-statement). your while loop will run exactly once no matter what, `break` will break the while loop and all other conditions lead to `loop = 'n'`. – Foocli Nov 06 '19 at 20:05

1 Answers1

0

Your problem is a logic problem here:

filename = input("Enter the name of the file you wish to process?: ")
with open(filename, "r") as file:
    ...
if FileCheck(filename) == 1:

You very clearly input a file name and then, without bothering to check its existence, try to open it. You don't check until you've finished reading the file.

The logic you expressed in your written description suggests that you want

filename = input("Enter the name of the file you wish to process?: ")
if FileCheck(filename):
    with open(filename, "r") as file:
        ...
Prune
  • 76,765
  • 14
  • 60
  • 81