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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM