python獲取文件日期

admin Python評論516字數 693閱讀模式

獲取文件日期可以使用以下幾種不同的實現方法:

1.使用os.path模塊的getmtime()函數獲取文件的修改時間,并將其轉換為日期格式:文章源自網吧系統維護-http://www.strong-digital.cn/11859.html

import os
import datetime
 
file_path = 'path_to_file'  # 文件路徑
 
mtime = os.path.getmtime(file_path)  # 獲取文件的修改時間
date = datetime.datetime.fromtimestamp(mtime)  # 將修改時間轉換為日期格式
 
print(date)

2.使用os.path模塊的getctime()函數獲取文件的創(chuàng)建時間,并將其轉換為日期格式:文章源自網吧系統維護-http://www.strong-digital.cn/11859.html

import os
import datetime
 
file_path = 'path_to_file'  # 文件路徑
 
ctime = os.path.getctime(file_path)  # 獲取文件的創(chuàng)建時間
date = datetime.datetime.fromtimestamp(ctime)  # 將創(chuàng)建時間轉換為日期格式
 
print(date)

3.使用os.path模塊的getatime()函數獲取文件的訪問時間,并將其轉換為日期格式:文章源自網吧系統維護-http://www.strong-digital.cn/11859.html

import os
import datetime
 
file_path = 'path_to_file'  # 文件路徑
 
atime = os.path.getatime(file_path)  # 獲取文件的訪問時間
date = datetime.datetime.fromtimestamp(atime)  # 將訪問時間轉換為日期格式
 
print(date)

注意:以上實現方法中的'path_to_file'需要根據實際情況替換為待處理文件的具體路徑。文章源自網吧系統維護-http://www.strong-digital.cn/11859.html 文章源自網吧系統維護-http://www.strong-digital.cn/11859.html

版權聲明:文章圖片資源來源于網絡,如有侵權,請留言刪除!!!
廣告也精彩
admin
  • 本文由 發(fā)表于 2024年4月27日 10:45:31
  • 轉載請務必保留本文鏈接:http://www.strong-digital.cn/11859.html
匿名

發(fā)表評論

匿名網友 填寫信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: