{"id":43,"date":"2023-05-15T15:55:53","date_gmt":"2023-05-15T15:55:53","guid":{"rendered":"http:\/\/77interactive.com\/?p=43"},"modified":"2023-05-15T16:56:34","modified_gmt":"2023-05-15T16:56:34","slug":"python-file-with-zip","status":"publish","type":"post","link":"http:\/\/77interactive.com\/?p=43","title":{"rendered":"Python File with Zip &#8211; Created Yesterday"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Look for file and folder created yesterday<\/h2>\n\n\n\n<p>The python script below gets the newest file and directory created yesterday. The file and directory must match a pattern. <\/p>\n\n\n\n<p>The file and directory are then zipped. The source file and directory remain on the drive.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\nimport datetime\nimport re\nimport shutil\nimport tarfile\n\ndef get_newest_in_previous_day(path):\n    previous_day_files = &#91;]\n    previous_day_dirs = &#91;]\n\n    previous_day = (datetime.datetime.now() - datetime.timedelta(days=1))\n    pattern = re.compile(r\"\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z(\\.pbm\\.json)?$\")\n\n    for name in os.listdir(path):\n        # Skip names that do not match the pattern\n        if not pattern.match(name):\n            continue\n\n        full_path = os.path.join(path, name)\n        timestamp_str = name.split('T')&#91;0]  # Extract the date part from the name\n        timestamp = datetime.datetime.strptime(timestamp_str, \"%Y-%m-%d\")\n\n        # Check if it's the previous day\n        if timestamp.date() == previous_day.date():\n            if os.path.isfile(full_path):\n                previous_day_files.append((timestamp, name))\n            elif os.path.isdir(full_path):\n                previous_day_dirs.append((timestamp, name))\n\n    # Sort by timestamp (newest first) and get the first name\n    previous_day_files.sort(reverse=True)\n    previous_day_dirs.sort(reverse=True)\n\n    newest_file = previous_day_files&#91;0]&#91;1] if previous_day_files else None\n    newest_dir = previous_day_dirs&#91;0]&#91;1] if previous_day_dirs else None\n\n    gzip_name = previous_day.strftime(\"%Y-%m-%d-Database.gzip\")\n    gzip_file_path = os.path.join(path, gzip_name)\n\n    # If both the file and directory exist, compress them\n    if newest_file and newest_dir:\n        with tarfile.open(gzip_file_path, \"w:gz\") as tar:\n            tar.add(os.path.join(path, newest_file))\n            tar.add(os.path.join(path, newest_dir))\n\n        print(\"File and directory were zipped.\")\n    else:\n        print(\"Nothing was zipped.\")\n\n    return newest_file, newest_dir\n\n\npath = \"\/mnt\/backups\/0300\/rs\"\nget_newest_in_previous_day(path)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Look for file and folder created yesterday The python script below gets the newest file and directory created yesterday. The file and directory must match a pattern. The file and directory are then zipped. The source file and directory remain on the drive.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[10,14,3],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-python","tag-backup","tag-files","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python File with Zip - Created Yesterday - 77 Interactive<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/77interactive.com\/?p=43\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python File with Zip - Created Yesterday - 77 Interactive\" \/>\n<meta property=\"og:description\" content=\"Look for file and folder created yesterday The python script below gets the newest file and directory created yesterday. The file and directory must match a pattern. The file and directory are then zipped. The source file and directory remain on the drive.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/77interactive.com\/?p=43\" \/>\n<meta property=\"og:site_name\" content=\"77 Interactive\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-15T15:55:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-05-15T16:56:34+00:00\" \/>\n<meta name=\"author\" content=\"Rudy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rudy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/77interactive.com\/?p=43#article\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/?p=43\"},\"author\":{\"name\":\"Rudy\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"headline\":\"Python File with Zip &#8211; Created Yesterday\",\"datePublished\":\"2023-05-15T15:55:53+00:00\",\"dateModified\":\"2023-05-15T16:56:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=43\"},\"wordCount\":50,\"keywords\":[\"backup\",\"files\",\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/77interactive.com\/?p=43\",\"url\":\"http:\/\/77interactive.com\/?p=43\",\"name\":\"Python File with Zip - Created Yesterday - 77 Interactive\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/#website\"},\"datePublished\":\"2023-05-15T15:55:53+00:00\",\"dateModified\":\"2023-05-15T16:56:34+00:00\",\"author\":{\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"breadcrumb\":{\"@id\":\"http:\/\/77interactive.com\/?p=43#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/77interactive.com\/?p=43\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/77interactive.com\/?p=43#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/77interactive.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python File with Zip &#8211; Created Yesterday\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/77interactive.com\/#website\",\"url\":\"http:\/\/77interactive.com\/\",\"name\":\"77 Interactive\",\"description\":\"Rudy&#039;s Code snippets\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/77interactive.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\",\"name\":\"Rudy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g\",\"caption\":\"Rudy\"},\"url\":\"http:\/\/77interactive.com\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python File with Zip - Created Yesterday - 77 Interactive","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/77interactive.com\/?p=43","og_locale":"en_US","og_type":"article","og_title":"Python File with Zip - Created Yesterday - 77 Interactive","og_description":"Look for file and folder created yesterday The python script below gets the newest file and directory created yesterday. The file and directory must match a pattern. The file and directory are then zipped. The source file and directory remain on the drive.","og_url":"http:\/\/77interactive.com\/?p=43","og_site_name":"77 Interactive","article_published_time":"2023-05-15T15:55:53+00:00","article_modified_time":"2023-05-15T16:56:34+00:00","author":"Rudy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rudy","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/77interactive.com\/?p=43#article","isPartOf":{"@id":"http:\/\/77interactive.com\/?p=43"},"author":{"name":"Rudy","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"headline":"Python File with Zip &#8211; Created Yesterday","datePublished":"2023-05-15T15:55:53+00:00","dateModified":"2023-05-15T16:56:34+00:00","mainEntityOfPage":{"@id":"http:\/\/77interactive.com\/?p=43"},"wordCount":50,"keywords":["backup","files","python"],"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/77interactive.com\/?p=43","url":"http:\/\/77interactive.com\/?p=43","name":"Python File with Zip - Created Yesterday - 77 Interactive","isPartOf":{"@id":"http:\/\/77interactive.com\/#website"},"datePublished":"2023-05-15T15:55:53+00:00","dateModified":"2023-05-15T16:56:34+00:00","author":{"@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"breadcrumb":{"@id":"http:\/\/77interactive.com\/?p=43#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/77interactive.com\/?p=43"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/77interactive.com\/?p=43#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/77interactive.com\/"},{"@type":"ListItem","position":2,"name":"Python File with Zip &#8211; Created Yesterday"}]},{"@type":"WebSite","@id":"http:\/\/77interactive.com\/#website","url":"http:\/\/77interactive.com\/","name":"77 Interactive","description":"Rudy&#039;s Code snippets","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/77interactive.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9","name":"Rudy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/77interactive.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e336b9aecd39b40691ff8ccfcd68506415072dbe8caffc0485b94a1bc22b774d?s=96&d=mm&r=g","caption":"Rudy"},"url":"http:\/\/77interactive.com\/?author=1"}]}},"_links":{"self":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":0,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}