f = open("hello.txt")
try:
forlineinf:
printline
finally:
f.close()对文件的打开和读写部分要分开处理异常:若文件未成功打开,则只需进行普通处理,不必进行close()操作,不要把open语句写到try语句块中
f = open("hello.txt")
try:
forlineinf:
printline
finally:
f.close()对文件的打开和读写部分要分开处理异常:若文件未成功打开,则只需进行普通处理,不必进行close()操作,不要把open语句写到try语句块中