【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'

时间:2022-07-27 08:12:20

【代码】

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方法打开,结果:

【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'

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()