{"id":804,"date":"2020-01-16T09:48:42","date_gmt":"2020-01-16T14:48:42","guid":{"rendered":"http:\/\/jasonralph.org\/?p=804"},"modified":"2020-01-22T20:59:43","modified_gmt":"2020-01-23T01:59:43","slug":"python-remove-files-that-match-pattern-older-than-n-days","status":"publish","type":"post","link":"https:\/\/jasonralph.org\/?p=804","title":{"rendered":"Python Remove Files That Match Pattern Older Than N Days"},"content":{"rendered":"<p>Neat little script that implements find in pure python, this can be passed different patterns and directories.  The script will walk the directories and match the patterns, it will then generate a list of files and get the ctime of each.  Some comparison is done against a date you set and removes them.  This is great for cleaning up application logs that clog up the filesystem. <\/p>\n<pre class=\"theme:solarized-dark lang:default decode:true \" >\r\n#!\/usr\/bin\/python3.5\r\n\r\nimport fnmatch\r\nimport os\r\nfrom datetime import datetime, timedelta\r\nfrom pathlib import Path\r\n\r\n# set variables for dirs to clean.\r\nlog_path = os.environ[\"LOG_PATH\"]\r\nuser_prod_home = str(Path.home())\r\n\r\n# set lists of dirs and patterns to clean\r\ndirs_to_clean = [log_path, user_prod_home]\r\npatterns = ['*.log', 'app_*']\r\n\r\n\r\n# function to loop and search patterns and rm files.\r\ndef find_files(dir_to_clean):\r\n    file_list = []\r\n    days_ago = datetime.now() - timedelta(days=60)\r\n    for root, dirs, files in os.walk(dir_to_clean):\r\n        for pattern in patterns:\r\n            for filename in fnmatch.filter(files, pattern):\r\n                file_list.append(os.path.join(root, filename))\r\n                file_list.sort()\r\n\r\n    for file in file_list:\r\n        file_ctime = datetime.fromtimestamp(os.path.getctime(file))\r\n        if file_ctime < days_ago:\r\n            if os.path.isfile(file):\r\n                try:\r\n                    print(\"Removing file :[{0}]\".format(file))\r\n                    os.remove(file)\r\n                except OSError as e:\r\n                    print('File Clean Up Failed: [{0}]'.format(e))\r\n\r\n\r\n# main function\r\ndef main():\r\n    for dirs in dirs_to_clean:\r\n        find_files(dirs)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    main()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Neat little script that implements find in pure python, this can be passed different patterns and directories. The script will walk the directories and match the patterns, it will then generate a list of files and get the ctime of each. Some comparison is done against a date you set and removes them. This is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[85,82,84,83,24,81],"class_list":["post-804","post","type-post","status-publish","format-standard","hentry","category-python","tag-files","tag-find","tag-match","tag-pattern","tag-python-2","tag-python3"],"_links":{"self":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=804"}],"version-history":[{"count":9,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions"}],"predecessor-version":[{"id":813,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions\/813"}],"wp:attachment":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}