所以我在这里得到了这段代码:
with open(os.path.join(Path, 'ki/lol/kappa/file.txt'), 'r') as masterfile:
如何添加一个告诉用户文件" file.txt"在这个目录中找不到?启动程序后,我收到一个名为IOError [Errno 2] No such file in this directory
的错误。如何显示"此处未找到文件的消息"而不是出现此错误?
答案 0 :(得分:0)
您使用try
语句。这称为异常处理。请参阅Python docs了解详情。
答案 1 :(得分:0)
你需要捕捉异常。
try:
with open(os.path.join(Path, 'ki/lol/kappa/file.txt'), 'r') as masterfile:
print "yay"
except IOError:
print "Oops, couldn't open the file"