python 异常处理
python">def main(): try: for line in readfile('lines.txt'): print(line.strip()) except IOError as e: print('could not open the file:',e) except ValueError as e: print('bad filename:',e) def readfile(filename): if filename.endswith(".txt"): fh = open(filename) return fh.readlines() else: raise ValueError("file must be ends with .txt") if __name__ == "__main__": main()