python获得文件创建时间和修改时间的方法

yipeiwu_com6年前Python基础

本文实例讲述了python获得文件创建时间和修改时间的方法。分享给大家供大家参考。具体如下:

这里需要用户从控制台输入文件路径

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)

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python 保存float类型的小数的位数方法

python保留两位小数: In [1]: a = 5.026 In [2]: b = 5.000 In [3]: round(a,2) Out[3]: 5.03 In [4]...

python实现监控linux性能及进程消耗性能的方法

本文以实例形式实现了python监控linux性能以及进程消耗性能的方法,具体实现代码如下: # -*- coding: utf-8 -*- """ Created on Tue J...

Python:type、object、class与内置类型实例

Python:type、object、class Python: 一切为对象 >>> a = 1 >>> type(a) <class'in...

python 对给定可迭代集合统计出现频率,并排序的方法

给定一个可迭代sequence,对其中的值进行出现次数统计: 方法1: def get_counts(sequence): counts = {} for x in sequen...

Python快速查找list中相同部分的方法

Python快速查找list中相同部分的方法

如下所示: l = [1, 2, 3, 5] l_one = [2, 8, 6, 10] print set(l) & set(l_one) 以上这篇Python快速查找lis...