python close file descriptor question

前端 未结 4 1525
日久生厌
日久生厌 2020-11-28 08:20

I think this question is more of a \"coding style\" rather than technical issue.

Said I have a line of code:

buf = open(\'test.txt\',\'r\').readlines         


        
4条回答
  •  情话喂你
    2020-11-28 08:59

    It will stay in memory until the garbage collector closes it. You should always explicitly close your file descriptors. Just do something like this:

    with open('test.txt', 'r') as f:
        buf = f.readlines()
    

提交回复
热议问题