Explicit way to close file in Python

﹥>﹥吖頭↗ 提交于 2021-02-07 11:45:11

问题


Please look at the following code:

for i in xrange(1,5000):
    with open(r'test\test%s.txt' % i, 'w') as qq:
        qq.write('aa'*3000)

It seems to be written according to all Python rules; files are closing after using. Seems to. But in fact it seems to recommend(!) system to close file, not to close it explicitly because when I'm looking on Resource monitor it shows a lot of open files . It gives me a lot of problems because in my script I use a lot of files and after a long time I got "Too many open files" error despite of 'closing' it from source code.

Is there some way to explicitly close file in Python? Or how can I check whether the file was really(!) closed or not?

Update: I've just tried with another monitoring tool - Handle from Sysinternals and it shows all correct and I trust it. So, it may be problem in Resource monitor itself.

Screenshot which shows files opened:

Resource Monitor with the script running


回答1:


Your code

for i in xrange(1, 5000):
    with open(r'test\test%s.txt' % i, 'w') as qq:
        qq.write('aa' * 3000)

is semantically exactly equivalent to

for i in xrange(1, 5000):
    qq = open(r'test\test%s.txt' % i, 'w')
    try:
        qq.write('aa' * 3000)
    finally:
        qq.close()

as using with with files is a way to ensure that the file is closed immediately after the with block is left.

So your problem must be somewhere else.

Maybe the version of the Python environment in use has a bug where fclose() isn't called due to some reason.

But you might try something like

try:
    qq.write('aa' * 3000)
finally:
    # qq.close()
    os.close(qq.fileno())

which does the system call directly.




回答2:


You should be able explicitly close the file by calling qq.close(). Also, python does not close a file right when it is done with it, similar to how it handles its garbage collection. You may need to look into how to get python to release all of its unused file descriptors. If it is similar to how it handles unused variable then it will tell the os that it is still using them, whether or not they are currently in use by your program.



来源:https://stackoverflow.com/questions/25176168/explicit-way-to-close-file-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!