【Selenium + Python】之如何获取最新的报告以及os.path.getmtime与os.path.getctime的区别
import os def new_file(test_dir): #列举test_dir目录下的所有文件(名),结果以列表形式返回。 lists=os.listdir(test_dir) #sort按key的关键字进行升序排序,lambda的入参fn为lists列表的元素,获取文件的最后修改时间,所以最终以文件时间从小到大排序 #最后对lists元素,按文件修改时间大小从小到大排序。 #获取最新文件的绝对路径,列表中最后一个值,文件夹+文件名 lists.sort(key=lambda fn:os.path.getmtime(test_dir+'\\'+fn)) file_path=os.path.join(test_dir,lists[-1]) return file_path #返回D:\pythontest\ostest下面最新的文件 print new_file('D:\\system files\\workspace\\selenium\\email126pro\\email126\\report')
最后再啰嗦一句,关于lambda的用法(python中单行的最小函数):
lambda函数也叫匿名函数,即,函数没有具体的名称。
key=lambda fn:os.path.getmtime(test_dir+'\\'+fn) #相当于 def key(fn): return os.path.getmtime(test_dir+'\\'+fn)
os.path.getmtime与os.path.getctime的区别:
import os import time file='/Volumes/Leopard/Users/Caroline/Desktop/1.mp4' os.path.getatime(file) #输出最近访问时间1318921018.0 os.path.getctime(file) #windows环境下是输出文件创建时间;如果是linux环境下ctime代表“状态时间” os.path.getmtime(file) #输出最近修改时间 time.gmtime(os.path.getmtime(file)) #以struct_time形式输出最近修改时间 os.path.getsize(file) #输出文件大小(字节为单位) os.path.abspath(file) #输出绝对路径'/Volumes/Leopard/Users/Caroline/Desktop/1.mp4' os.path.normpath(file) #输出'/Volumes/Leopard/Users/Caroline/Desktop/1.mp4'
附录:
不积跬步,无以致千里;不集小流,无以成江海。
如转载本文,请还多关注一下我的博客:https://www.cnblogs.com/Owen-ET/;
我的Github地址:https://github.com/Owen-ET————————————
无善无恶心之体, 有善有恶意之动, 知善知恶是良知, 为善去恶是格物。
« 上一篇: 【Python + Selenium】初次用IE浏览器之报错:selenium.common.exceptions.WebDriverException: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones.
» 下一篇: 【Selenium + Python】路径报错之OSError: [Errno 22] Invalid argument: './t/report/2018-03-23_11:03:12_report.html'
» 下一篇: 【Selenium + Python】路径报错之OSError: [Errno 22] Invalid argument: './t/report/2018-03-23_11:03:12_report.html'
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App