当前位置: 首页 > 图灵资讯 > 行业资讯> python没有报错提示

python没有报错提示

发布时间:2025-12-08 16:32:41

Python中没有报错提示的代码示例:

defcount_words(filename):
try:
withopen(filename)asf_obj:
contents=f_obj.read()
exceptFileNotFoundError:
pass
else:
words=contents.spilt()
num_words=len(words)
print("Thisfile"+filename+'hasabout'+str(num_words)+'words.')
===========================================================================================
defcount_words(filename):
try:
withopen(filename)asf_obj:
contents=f_obj.read()
exceptFileNotFoundError:
message='sorry,thefile'+filename+'doesnotexits'
print(message)
else:
words=contents.spilt()
num_words=len(words)
print("Thisfile"+filename+'hasabout'+str(num_words)+'words.')

原因:

其中使用了 try-except-else 代码块,except是在try代码中出现错误时执行的,而except中的代码是pass,即在try中出现错误时直接通过,不为用户编写任何建议和help。

相应报错的代码也写在横线下面。

有关Python的更多信息,请注意:Python自学网!!

相关文章

python没有报错提示

python没有报错提示

2025-12-08
python能实现并行吗

python能实现并行吗

2025-12-08
python判断两个路径是否相等

python判断两个路径是否相等

2025-12-08
python如何分析灰度

python如何分析灰度

2025-12-08
python如何给图加标签

python如何给图加标签

2025-12-08
python如何调节音量大小

python如何调节音量大小

2025-12-05