用Python读取文件的创建时间、最后更新时间并显示
以下是一个可以读取PCAP文件的创建时间和最后更新时间的Python程序:
import os
import datetime
def get_pcap_file_info(filepath):
"""
获取PCAP文件的创建时间和最后更新时间
"""
# 检查文件是否存在
if not os.path.exists(filepath):
raise ValueError("文件不存在")
# 获取文件信息
file_stat = os.stat(filepath)
create_time = datetime.datetime.fromtimestamp(file_stat.st_ctime)
update_time = datetime.datetime.fromtimestamp(file_stat.st_mtime)
return create_time, update_time
# 示例程序
if __name__ == "__main__":
pcap_file = "example.pcap"
try:
create_time, update_time = get_pcap_file_info(pcap_file)
print(f"文件{pcap_file}的创建时间为:{create_time}")
print(f"文件{pcap_file}的最后更新时间为:{update_time}")
except ValueError as e:
print(f"获取文件信息失败:{str(e)}")
在上面的程序中,get_pcap_file_info
函数接收一个PCAP文件的完整路径作为参数,返回PCAP文件的创建时间和最后更新时间。主程序中使用get_pcap_file_info
函数获取示例文件example.pcap
的创建时间和最后更新时间,并打印输出。
运行上述程序后,将会得到类似如下的输出:
文件example.pcap的创建时间为:2022-01-01 10:00:00
文件example.pcap的最后更新时间为:2022-01-02 14:30:00
其中,时间的格式根据计算机的区域设置可能会有所不同。