python - get the modification time and creation time of a file

本文深入探讨了Unix系统中文件的时间管理,特别关注了文件的创建时间与修改时间的区别,并提供了使用Python获取这些时间戳的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

as in Unix, you know that the creation time is marked as the creation time of the inode, while the m time (so called modification time) is also the time to the inode. (creation of inode happens when you first create file/directory), while the modification bit of the inode indicate add/removing content to a directory or changing the content of a file);

 

 

To get the mtime and ctime, you can use the time module together with the os.path module, here is the cdoe that best show how you can query some time stamp associated with a file.

 

 

import os.path, time
import exceptions

class TypeError (Exception):
    pass


if __name__ == '__main__':
  if (len(os.sys.argv) < 1):
      raise TypeError()
  else:
      print "os.sys.argv[0]: %s" % os.sys.argv[0] # os.sys.argv[0] is the current file, in this case, file_ctime.py
  f = os.sys.argv[0]
  mtime = time.ctime(os.path.getmtime(f))
  ctime = time.ctime(os.path.getctime(f))
  print "Last modified : %s, last created time: %s" % (mtime, ctime)
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值