
python – 逐行文件处理,for-loop vs with
我正在努力了解这些之间的折衷/差异 打开文件进行逐行处理的方法 with open(data.txt) as inf: for line in inf: #etc VS for line in open(data.txt): # etc 我明白使用确保文件关闭时 “with-block”(suite?)被退出(或异常被反驳).所以我一直在使用,因为我
我正在努力了解这些之间的折衷/差异 打开文件进行逐行处理的方法 with open(data.txt) as inf: for line in inf: #etc VS for line in open(data.txt): # etc 我明白使用确保文件关闭时 “with-block”(suite?)被退出(或异常被反驳).所以我一直在使用,因为我