Python unclosed resource: is it safe to delete the file?

前端 未结 1 1392
遇见更好的自我
遇见更好的自我 2020-12-10 14:10

Googled my way around this issue, but didn\'t find any solutions. I\'m running Python 3.3 with Eclipse and PyDev plugin, and when I run any Python project, I get the followi

相关标签:
1条回答
  • 2020-12-10 14:40

    This ResourceWarning means that you opened a file, used it, but then forgot to close the file. Python closes it for you when it notices that the file object is dead, but this only occurs after some unknown time has elapsed. Thus in recent versions, Python also prints a ResourceWarning when it does that. It is a way for you to quickly identify where the unclosed files are, and properly close them. It might be important on some platforms which cannot have more than N files opened at the same time (e.g. 1024). Also, specifically on Windows, you cannot do some operations with a file if it's still open (e.g. deleting it).

    In this case, the line in the file update_checker.py needs to be fixed to say:

    with open(filename, 'rb') as f:   # will close() when we leave this block
        permacache = pickle.load(f)
    
    0 讨论(0)
提交回复
热议问题