Python FileNotFound [英] Python FileNotFound
问题描述
我正在尝试制作一个脚本,读取数独的解决方案和阻止它们是否正确。
我需要的东西:
1]提示用户输入文件/文件路径包括数独数字。它的一个.txt文件为9行和列。仅包含数字。
2]有某种错误处理。
3]然后,如果数独是有效的,我应该使用与原始输入文件相同的格式创建一个新的文本文件,前缀Correct _
我还没有完全完成该程序,但是当我输入虚假的路径或文件名时,我会收到此错误。
您好,Sudoku valitator,
请输入您的文件的路径,然后按'Enter':example.txt#这是一个非现有文件,用于测试错误异常
'Traceback(最近的最后一次调用):
文件C:/Users/FEDROS/Desktop/bs.py,第9行在< module>
sudoku = open(prompt,'r')readlines()
FileNotFoundError:[Errno 2]没有这样的文件或目录:'example.txt'
这是我的脚本:
while True:
try:
prompt = input(\你好,Sudoku valitator,
\\\
\\\
请输入您的文件的路径然后按'Enter':)
break
except(FileNotFoundError,IOError):
print(错误的文件或文件路径)
sudoku = open提示,'r')readlines()
def check(game):
n = len(game)
if n < (1):
return False
对于范围(0,n)中的i:
horizontal = []
vertical = []
for k在范围(0,n):
如果游戏[k] [i]在垂直:
返回(文件检查错误,您的选项是错误的!)
如果游戏[i] [k]在水平上:
return(文件检查错误,您的选项是错误的!)
horizontal.append(game [i] [k])
return(文件检查错误,您的选项是正确的!)
print(check(sudoku))
谢谢,任何建议或帮助将不胜感激。
try
block应该是开放的。不在提示。
while True:
prompt = input(\你好,Sudoku valitator
\\\
\\\
请输入文件的路径,然后按'Enter':)
try:
sudoku = open(prompt,'r')readlines()
除了FileNotFoundError:
print(错误的文件或文件路径)
else:
break
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.
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
这篇关于Python FileNotFound的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!