ResourceWarning:运行 python selenium unittest 时出现未关闭的文件错误

sh *_*it3 6 python selenium python-3.x selenium-webdriver python-unittest

运行 python selenium unittest 时,我看到一个错误:

/usr/lib/python3.6/unittest/case.py:605: ResourceWarning: unclosed file <_io.TextIOWrapper name='/home/newuser/scable/HTML_Test_Runner_ReportTest.html' mode='w' encoding='UTF-8'>
  testMethod()
.
----------------------------------------------------------------------
Ran 1 test in 0.006s

OK
sys:1: ResourceWarning: unclosed <socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('127.0.0.1', 42230), raddr=('127.0.0.1', 34255)>
Run Code Online (Sandbox Code Playgroud)

这个错误是什么意思?

Deb*_*anB 1

这个错误信息...

/usr/lib/python3.6/unittest/case.py:605: ResourceWarning: unclosed file
Run Code Online (Sandbox Code Playgroud)

...意味着在尝试访问未关闭的文件时出现了ResourceWarning


此问题特定于,因为ResourceWarnings仅在Python3中发出。最可能的原因是,您打开了一个文件并使用了它,但随后忘记关闭该文件。尽管当Python注意到文件对象已死亡时,它会代表您自动关闭打开的文件,但这会在经过一段相当长的时间后发生。

如果您尝试在仍打开的文件中执行某些操作(例如尝试删除文件),则在上也可能会出现此问题。

HTML_Test_Runner_ReportTest.html但是,当您在文件仍在处理中时尝试打开该文件时,似乎会出现此问题。

一个简单的解决方案是等待HTML_Test_Runner_ReportTest.html文件完全处理然后访问它。