獲取文件日期可以使用以下幾種不同的實現方法:
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
版權聲明:文章圖片資源來源于網絡,如有侵權,請留言刪除!!!
評論