Python文件与关闭

时间:2017-03-17 14:57:44

标签: python python-3.x garbage-collection

如果我写这样的代码,会发生什么?文件对象会自动关闭吗?

# Do something before
# Make the value = 1 when the number in file.
for line in open('input_file.txt', 'r'):
    index_list[int(line)] = 1
# Do something later

或者我必须编写这样的代码?

with open('xxx.txt', 'r') as f:
    # Do something

0 个答案:

没有答案