python封装上传图片方法执行时有告警【ResourceWarning: Enable tracemalloc to get the object allocation traceback5】
封装上传文件的时候需要的问题,使用 python os 模块
打开cmd 并执行上传文件操作之后,文件可以正常上传文件,但是会报错
ResourceWarning: unclosed file <_io.TextIOWrapper name=3 encoding='cp936'> os.popen(cmd) ResourceWarning: Enable tracemalloc to get the object allocation traceback C:\Users\admin\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py:883: ResourceWarning: subprocess 25096 is still running ResourceWarning, source=self) ResourceWarning: Enable tracemalloc to get the object allocation traceback [2021-08-26 15:23:40,781] base_page.py->upload_file line:631 [ERROR] : 当前脚本不能执行原因是: an integer is required (got type str) [2021-08-26 15:23:40,783] base_page.py->screenshot_as_file line:414 [ERROR] : 截图失败,原因是: unsupported operand type(s) for +: 'NoneType' and 'str' . ---------------------------------------------------------------------- Ran 1 test in 35.989s OK
第一个解决io文件的告警需要将打开的io对象关闭即可
def upload_file(self, browser, file_path, file_exe_path=local_config.get_upload_path): """上传文件""" try: """上传文件""" exe_file = file_exe_path cmd = "\"" + exe_file + "\"" + " " + "\"" + browser + "\"" + " " + "\"" + file_path + "\"" cd = os.popen(cmd) self.wait(2) cd.close() # 关闭io对象 except Exception as e: logger.error("当前脚本不能执行原因是: %s" % e) self.screenshot_as_file()
第二个告警
# 解决错误 ResourceWarning: Enable tracemalloc to get the object allocation traceback5
在执行文件的前面加上这段话就行了
import warnings
@classmethod def setUpClass(cls) -> None: # 解决错误 ResourceWarning: Enable tracemalloc to get the object allocation traceback5 warnings.simplefilter('ignore', ResourceWarning)
关于上传文件的封装请看我另一个文章
https://www.cnblogs.com/yushengaqingzhijiao/p/15192685.html