我有一个类似的功能:
def func(filename):
with open(filename) as f:
return [line.split('\t')[0] for line in f]
即使有“突然”函数返回,“with”语句是否也会关闭文件?我可以忽略“with”语句吗?即这样做是否安全且等效(从内存泄漏的角度来看),
def func(filename):
return [line.split('\t')[0] for line in open(filename)]
?