Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีตั้งค่าวันที่สร้างและแก้ไขไฟล์ด้วย Python

ใน Windows ใช้ os.path.getctime(file_path) สำหรับ creation time บน UNIX ใช้ st_birthtime จาก stat tuple (ไม่รองรับ Linux ทุกตัว)

ตัวอย่าง Windows

>>> import os
>>> print(os.path.getctime('my_file'))
1505928271.0689342

ค่าเป็นวินาทีตั้งแต่ epoch สำหรับ UNIX:

import os
stat = os.stat(path_to_file)
try:
    print(stat.st_birthtime)
except AttributeError:
    # Linux ไม่รองรับ creation timeง่าย ใช้ mtime แทน
    print(stat.st_mtime)

Modification Time (Cross-platform)

ใช้ os.path.getmtime(path) ได้ทุกแพลตฟอร์ม:

>>> import os
>>> print(os.path.getmtime('my_file'))
1505928275.3081832