0

Need to activate a while loop if a user inputted file is found, the in the "while" loop have a "try" that opens the file if the file is found

def main():
  customer_file = input("Enter the name of the file ")
  while _____:
    try:
      input_file = open(customer_file, "r")
    except FileNotFoundError:
      print("Invalid file Name")

  line = input_file.readline()
main()
Evekk20
  • 9
  • 1

1 Answers1

0

For example:

def main():
    while True:
        customer_file = input("Enter the name of the file ")
        try:
            with open(customer_file, "r") as input_file:
                # better to use contex manager (with open...) to handle the file
                line = input_file.readline()
            break
        except FileNotFoundError:
            print("Invalid file Name")


main()
chepner
  • 497,756
  • 71
  • 530
  • 681
ruohola
  • 21,987
  • 6
  • 62
  • 97