【Selenium】【BugList9】windows環境,fp = open("./"+ time.strftime("%Y-%m-%d %H:%M:%S") + " result.html",'wb'),報錯:OSError: [Errno 22] Invalid argument: './2018-09-05 10:29:32 result.html'


【代碼】

if __name__=="__main__":
    suite = unittest.TestSuite()
    suite.addTest(Baidu("test_baidu"))
    
 url ="./"+ time.strftime("%Y-%m-%d %H:%M:%S") + " result.html"
    fp = open(url,'wb')
    runner = HTMLTestRunner(stream = fp,
                            title = 'Report of Baidu_search',
                            description = 'TestCase run')
    
    runner.run(suite)
    fp.close()

 

【報錯】

==================== RESTART: C:/Users/admin/Desktop/1.py ====================
Traceback (most recent call last):
File "C:/Users/admin/Desktop/1.py", line 31, in <module>
fp = open("./"+ time.strftime("%Y-%m-%d %H:%M:%S") + " result.html",'wb')
OSError: [Errno 22] Invalid argument: './2018-09-05 10:29:32 result.html'

 

【解決思路】

1.python IDLE中:open("./result.html",'wb'),成功

2.去掉“%Y-%m-%d %H:%M:%S”中空格,仍報錯 

3.修改“%Y-%m-%d %H:%M:%S”中‘:’ 為 ‘-’,成功—>冒號 ‘:’ 引起

4.在python IDLE中,執行 time.strftime("%Y-%m-%d %H:%M:%S") 可成功,但open方法執行卻不行?

5.在Windows下,創建文件名帶冒號的文件,再用open方法打開,結果:

 

6.結論:Windows環境,不能創建文件名帶英文冒號:的文件,所以open方法創建/打開文件失敗

 

【代碼-修改后】

if __name__=="__main__":
    suite = unittest.TestSuite()
    suite.addTest(Baidu("test_baidu"))
    
    url ="./"+ time.strftime("%Y-%m-%d %H_%M_%S") + " result.html"
    fp = open(url,'wb')
    runner = HTMLTestRunner(stream = fp,
                            title = 'Report of Baidu_search',
                            description = 'TestCase run')
    
    runner.run(suite)
    fp.close()

 

【Ending】

微信公眾號“粒粒的測試筆記


免責聲明!

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



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