python腳本獲取文件的創建於修改日期並計算時間差


由於在計算一個算法的運行時間的時候,需要將文件的創建日期與修改日期讀取到,然后計算兩者之差,在網上搜索了相關的程序之后,自己又修改了一下,把代碼貼在這里,供以后查閱使用,也希望可以幫到其他人。

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Mon Dec 12 14:59:46 2016
 4 
 5 @author: shenruixue
 6 
 7 to calculate size after filter in conv and pool
 8 """
 9 import os.path, time
10 import exceptions
11 class TypeError (Exception):
12   pass
13 if __name__ == '__main__':
14  file_srx = open("train_1920_1080.set")#其中包含所有待計算的文件名
15  line = file_srx.readline()
16  diff_time_all = 0
17  while line:
18   f = line[:-1]    # 除去末尾的換行符
19   print (f)
20   print ('***********************************************************')
21   mtime = time.ctime(os.path.getmtime(f))
22   ctime = time.ctime(os.path.getctime(f))
23   mtime_s = (os.path.getmtime(f))
24   ctime_s = (os.path.getctime(f))
25   print "Last modified : %s, last created time: %s" % (mtime, ctime)
26   diff_time = (int(mtime_s) - int(ctime_s))
27   diff_time_all = diff_time_all + diff_time
28   print "diff time is ", diff_time
29   line = file_srx.readline() 
30  print "diff_time_all is ", diff_time_all
31  file_object = open('train_1920_1080.txt', 'w')
32  file_object.write(str(diff_time_all))
33  file_object.close( )

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM