0

I need to create a loop that prompts a user to enter a file name that they wish to be processed. If the file does not exist, an error message should show up saying "The file (filename) does not exist!". After the error message is prompted to the user the loop should ask the user to enter the name of a file they wish to be processed or to exit the program.

loop = 'y'
while loop == 'y':
    filename = input("Enter the name of the file you wish to process?: ")
    if FileNotFoundError is True:
        print("The file" + filename + "could not be found!")
        quit = input("Enter the name of the file you wish to process or type exit to quit: ")
        if quit == 'exit':
            loop = 'n'

I'm stuck in the first part of the loop. I'll input a file that either exists or not and it will just keep asking me the file I wish to process. I'm also sure that the following code is a longer version of a try except block that probably does not work. New to python and coding in general so this seems like a noob mistake.

Avenius
  • 21
  • 7
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Georgy Nov 06 '19 at 16:15

3 Answers3

0

Something like this should get you going.

def try_again_check(a: str) -> bool:
    if a == "y":
        return True
    return False

while True:
    try:
        filename = input("Enter the name of the file you wish to process?: ")
        with open(filename,'r') as file:
            DoSomething()
        break

    except FileNotFoundError():
        print("message to user")
        if try_again_check(input("Do you wish to continue: ")): 
            continue
        break
John Dowling
  • 582
  • 6
  • 23
0

To do this you should define a function that returns something based on if the file worked or not. I found an implementation Here. This should work

def FileCheck(fn):
        try:
          open(fn, "r")
          return 1
        except IOError:
          print "Error: File does not appear to exist."
          return 0

loop = 'y'
while loop == 'y':
    filename = input("Enter the name of the file you wish to process?: ")
    if FileCheck(filename) == 1:
        loop = 'n'
    else: 
        print("file not found")
CEWeinhauer
  • 123
  • 1
  • 9
  • 1
    I edited the else function and it's working how it should be! Thank you for the help and outline on how to create loops like this! – Avenius Nov 06 '19 at 15:31
  • 1
    @KeghanPettit, if this solved your question, consider selecting it as the accepted solution with the green check mark to the left of the post. – SyntaxVoid Nov 07 '19 at 23:12
-1

First off, I see that there is no else to the FileNotFoundError so it will continue to loop even if the filename is valid.

     loop = 'y'
        while loop == 'y':
            filename = input("Enter the name of the file you wish to process?: ")
            if FileNotFoundError is True:
                print("The file" + filename + "could not be found!")
                quit = input("Enter the name of the file you wish to process or type 
    exit to quit: ")
                if quit == 'exit':
                    loop = 'n'
             else:
# do something with the appropriately entered filename
# loop = 'n' to stop the loop
# break

Let me know if that doesn't address your issue and some more specifics and I can try to help more.

Hot Whiskey
  • 85
  • 1
  • 9
  • `FileNotFoundError` is an `Exception` type. This code, like the OPs, does no checking to see if the file exists. `if FileNotFoundError is True` is not doing what you think it's doing and will always evaluate to True (because `bool(Exception)` is *always* True). – SyntaxVoid Nov 06 '19 at 15:33