用Python讀取文件的創(chuàng)建時(shí)間、最后更新時(shí)間并顯示
以下是一個(gè)可以讀取PCAP文件的創(chuàng)建時(shí)間和最后更新時(shí)間的Python程序:
import os
import datetime
def get_pcap_file_info(filepath):
? ?"""
? ?獲取PCAP文件的創(chuàng)建時(shí)間和最后更新時(shí)間
? ?"""
? ?# 檢查文件是否存在
? ?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}的創(chuàng)建時(shí)間為:{create_time}")
? ? ? ?print(f"文件{pcap_file}的最后更新時(shí)間為:{update_time}")
? ?except ValueError as e:
? ? ? ?print(f"獲取文件信息失?。簕str(e)}")
在上面的程序中,get_pcap_file_info
函數(shù)接收一個(gè)PCAP文件的完整路徑作為參數(shù),返回PCAP文件的創(chuàng)建時(shí)間和最后更新時(shí)間。主程序中使用get_pcap_file_info
函數(shù)獲取示例文件example.pcap
的創(chuàng)建時(shí)間和最后更新時(shí)間,并打印輸出。
運(yùn)行上述程序后,將會(huì)得到類似如下的輸出:
文件example.pcap的創(chuàng)建時(shí)間為:2022-01-01 10:00:00
文件example.pcap的最后更新時(shí)間為:2022-01-02 14:30:00
其中,時(shí)間的格式根據(jù)計(jì)算機(jī)的區(qū)域設(shè)置可能會(huì)有所不同。
標(biāo)簽: