我有一个任务是编写一个“检测文件是否可读”的 Python 脚本。
我不知道应该运行哪些异常。假设输入文件是一个文本文件,带有扩展名*.txt
我应该提出什么例外?我怀疑应该有多个。目前,我有:
with open('example_file.txt") as textfile:
if not textfile.lower().endswith('.txt'):
raise argparse.ArgumentTypeError(
'Not a text file! Argument filename must be of type *.txt')
return textfile
但是,这只检查文件扩展名。我还能检查什么?Python中文件I/O的标准是什么?