python filenotfounderror,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

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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值