[python-3.x] Python FileNotFound

I am fairly new to python.

I am trying to make a script that will read sudoku solutions and determent if they are correct or not.

Things I need:

1] Prompt the user to enter a file/file path which includes the sudoku numbers. Its a .txt file of 9 rows and columns. Consist only of numbers.

2] Have some kind of an error handling.

3] Then, if the sudoku is valid, i should create a new text file using the same format as the original input file with the prefix "Correct_"

I have not fully finished the program, but I get this error when I put a false path or file name.

 Hello to Sudoku valitator,

 Please type in the path to your file and press 'Enter': example.txt #This is a non existing file, to test the Error Exception
    'Traceback (most recent call last):
  File "C:/Users/FEDROS/Desktop/bs.py", line 9, in <module>
    sudoku = open(prompt, 'r').readlines()
FileNotFoundError: [Errno 2] No such file or directory: 'example.txt'

Here is my script:

while True:
    try:
        prompt = input("\n Hello to Sudoku valitator,"
    "\n \n Please type in the path to your file and press 'Enter': ")
        break
    except (FileNotFoundError, IOError):
        print("Wrong file or file path")

sudoku = open(prompt, 'r').readlines()

def check(game):
    n = len(game)
    if n < (1):
        return False

    for i in range(0, n):
        horizontal = []
        vertical = []
        for k in range(0, n):

            if game[k][i] in vertical:
                return ("File checked for errors. Your options are wrong!")
            vertical.append(game[k][i])

            if game[i][k] in horizontal:
                return ("File checked for errors. Your options are wrong!")
            horizontal.append(game[i][k])
    return ("File checked for errors. Your options are correct!")

print (check(sudoku))

Thanks, any advice or help will be appreciated.

This question is related to python-3.x exception-handling file-not-found

The answer is


try block should be around open. Not around prompt.

while True:
    prompt = input("\n Hello to Sudoku valitator,"
    "\n \n Please type in the path to your file and press 'Enter': ")
    try:
        sudoku = open(prompt, 'r').readlines()
    except FileNotFoundError:
        print("Wrong file or file path")
    else:
        break

Examples related to python-3.x

Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation Replace specific text with a redacted version using Python Upgrade to python 3.8 using conda "Permission Denied" trying to run Python on Windows 10 Python: 'ModuleNotFoundError' when trying to import module from imported package What is the meaning of "Failed building wheel for X" in pip install? How to downgrade python from 3.7 to 3.6 I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? Iterating over arrays in Python 3 How to upgrade Python version to 3.7?

Examples related to exception-handling

Catching FULL exception message Spring Resttemplate exception handling How to get exception message in Python properly Spring Boot REST service exception handling java.net.BindException: Address already in use: JVM_Bind <null>:80 Python FileNotFound The process cannot access the file because it is being used by another process (File is created but contains nothing) Java 8: Lambda-Streams, Filter by Method with Exception Laravel view not found exception How to efficiently use try...catch blocks in PHP

Examples related to file-not-found

Python FileNotFound Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory C# windows application Event: CLR20r3 on application start Linux error while loading shared libraries: cannot open shared object file: No such file or directory