{"id":109,"date":"2017-07-18T21:16:04","date_gmt":"2017-07-18T19:16:04","guid":{"rendered":"http:\/\/developer.ps\/?p=109"},"modified":"2026-09-22T20:52:17","modified_gmt":"2026-09-22T18:52:17","slug":"python-get-files-modification-date-within-a-period-of-time","status":"publish","type":"post","link":"https:\/\/developer.ps\/index.php\/2017\/07\/18\/python-get-files-modification-date-within-a-period-of-time\/","title":{"rendered":"Python &#8211; Get files modification date within a period of time"},"content":{"rendered":"<p><!--more--><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">#!\/usr\/bin\/env python\r\n\r\nimport os, sys, time\r\nfrom datetime import timedelta, datetime\r\n\r\ntotalFiles1 = 0;\r\ntotalFilesSize1 = 0;\r\ntotalFiles2 = 0;\r\ntotalFilesSize2 = 0;\r\ndays = 0;\r\ndaysts = 0;\r\n\r\ndef checkModificationDates(path):\r\n    global totalFiles1, totalFilesSize1,totalFiles2, totalFilesSize2, days, daysts\r\n    totalsize1 = float();\r\n    files1 = 0;\r\n    totalsize2 = float();\r\n    files2 = 0;\r\n     \r\n    for directory, subdirectories, filenames in os.walk(path):\r\n        for filename in filenames:\r\n            full_filename = os.path.join(directory, filename);\r\n            unix_timestampc = float(os.path.getmtime(full_filename));\r\n            mod = time.ctime(os.path.getmtime(full_filename)) ;\r\n            filesize = os.stat(full_filename).st_size;\r\n            \r\n            if unix_timestampc &gt;= daysts:\r\n                totalsize1 += filesize;\r\n                files1 += 1\r\n                totalFilesSize1 += filesize;\r\n                totalFiles1 += 1;               \r\n            else:\r\n                totalsize2 += filesize;\r\n                files2 += 1;\r\n                totalFilesSize2 += totalsize2;\r\n                totalFiles2 += 1;\r\n    \r\n    print(\"Files in %s modified within %s days: [%s files, %s bytes]\" % (path,days, files1,totalsize1));\r\n    print(\"Total files number in %s : %s size %s bytes\" % (path,files1+files2,totalsize1+totalsize2));\r\n    print(\"\\n******************************************\\n\");\r\n    \r\nif __name__ == '__main__':\r\n    if len(sys.argv) &gt; 2:\r\n        days = int(\"0\" + sys.argv[1]);\r\n        now = time.time();\r\n        daysts = now - 60*60*24*days;\r\n        folders = sys.argv[2:]\r\n        for i in folders:\r\n            # Iterate the folders given\r\n            if os.path.exists(i):\r\n                checkModificationDates(i)                \r\n            else:\r\n                print('%s is not a valid path, please verify' % i)\r\n                sys.exit()\r\n        \r\n        print(\"Files in all pathes modified within %s days: [%s files, %s bytes]\" % (days, totalFiles1,totalFilesSize1));\r\n        print(\"Total files number all pathes: %s size %s bytes\" % (totalFiles1+totalFiles2,totalFilesSize1+totalFilesSize2));\r\n    else:\r\n        print('Usage: days path1 path2 path3...');<\/pre>\n<p>Usage:<\/p>\n<p>&nbsp;<\/p>\n<p>python filename.py [days] [path1] [path2] [path3] &#8230;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":2,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/posts\/109\/revisions\/111"}],"wp:attachment":[{"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.ps\/index.php\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}