{"id":239,"date":"2023-07-12T17:09:04","date_gmt":"2023-07-12T17:09:04","guid":{"rendered":"http:\/\/77interactive.com\/?p=239"},"modified":"2023-08-02T16:56:59","modified_gmt":"2023-08-02T16:56:59","slug":"python-first-of-the-month-files","status":"publish","type":"post","link":"http:\/\/77interactive.com\/?p=239","title":{"rendered":"Python &#8211; First of the Month Files"},"content":{"rendered":"\n<p>The following script uploads files from the first of the month via S3<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\r\nimport datetime\r\nimport re\r\nimport subprocess\r\nimport argparse\r\n\r\ndef get_newest_in_first_day(path, s3_target_path):\r\n    \"\"\"\r\n    This function scans a given directory for files and directories that were created on the first day of the current month.\r\n    It then uploads the newest file and directory to a specified S3 target path using the s3cmd tool.\r\n\r\n    Args:\r\n        path (str): The path of the directory to scan.\r\n        s3_target_path (str): The S3 path to upload the file and directory to.\r\n\r\n    Returns:\r\n        Tuple&#91;str, str]: The names of the newest file and directory, if they exist.\r\n    \"\"\"\r\n    first_day_files = &#91;]\r\n    first_day_dirs = &#91;]\r\n\r\n    today = datetime.datetime.now()\r\n\r\n    # Calculate the first day of the current month\r\n    first_day_of_month = today.replace(day=1)\r\n\r\n    pattern = re.compile(r\"\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z(\\.pbm\\.json)?$\")\r\n\r\n    for name in os.listdir(path):\r\n        # Skip names that do not match the pattern\r\n        if not pattern.match(name):\r\n            continue\r\n\r\n        full_path = os.path.join(path, name)\r\n        timestamp_str = name.split('T')&#91;0]  # Extract the date part from the name\r\n        timestamp = datetime.datetime.strptime(timestamp_str, \"%Y-%m-%d\")\r\n\r\n        # Check if it's the first day of the month\r\n        if timestamp.date() == first_day_of_month.date():\r\n            if os.path.isfile(full_path):\r\n                first_day_files.append((timestamp, name))\r\n            elif os.path.isdir(full_path):\r\n                first_day_dirs.append((timestamp, name))\r\n\r\n    # Sort by timestamp (newest first) and get the first name\r\n    first_day_files.sort(reverse=True)\r\n    first_day_dirs.sort(reverse=True)\r\n\r\n    newest_file = first_day_files&#91;0]&#91;1] if first_day_files else None\r\n    newest_dir = first_day_dirs&#91;0]&#91;1] if first_day_dirs else None\r\n\r\n    # If both the file and directory exist, upload them\r\n    if newest_file and newest_dir:\r\n        s3_config_path = \".\/s3config.txt\"\r\n        subprocess.call(&#91;\"s3cmd\", \"put\", \"--config\", s3_config_path, os.path.join(path, newest_file), s3_target_path])\r\n        subprocess.call(&#91;\"s3cmd\", \"put\", \"--recursive\", \"--config\", s3_config_path, os.path.join(path, newest_dir), s3_target_path])\r\n        print(f\"Uploaded file: {newest_file}\")\r\n        print(f\"Uploaded directory: {newest_dir}\")\r\n    else:\r\n        print(\"Nothing was uploaded.\")\r\n\r\n    return newest_file, newest_dir\r\n\r\ndef main():\r\n    parser = argparse.ArgumentParser(description='Process some paths.')\r\n    parser.add_argument('path', type=str, help='The path of the directory to scan')\r\n    parser.add_argument('s3_target_path', type=str, help='The S3 path to upload the file and directory to')\r\n\r\n    args = parser.parse_args()\r\n\r\n    get_newest_in_first_day(args.path, args.s3_target_path)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    main()\r\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">With Notification Email<\/h2>\n\n\n\n<p>The following version sends an email with the results<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\r\nimport datetime\r\nimport re\r\nimport subprocess\r\nimport argparse\r\nimport smtplib\r\nfrom email.mime.text import MIMEText\r\n\r\ndef send_email(subject, content):\r\n    msg = MIMEText(content)\r\n    msg&#91;'Subject'] = subject\r\n    msg&#91;'From'] = 'youremail@yourdomain.com'\r\n    msg&#91;'To'] = 'receiver@yourdomain.com'\r\n\r\n    # setup the SMTP server\r\n    server = smtplib.SMTP('your-smtp-server.com', 587) # use your smtp server\r\n    server.starttls()\r\n\r\n    server.send_message(msg)\r\n\r\n    server.quit()\r\n\r\ndef get_newest_in_first_day(path, s3_target_path):\r\n    # &#91;The function body remains the same]\r\n\r\n    if newest_file and newest_dir:\r\n        # &#91;The same upload code]\r\n        print(f\"Uploaded file: {newest_file}\")\r\n        print(f\"Uploaded directory: {newest_dir}\")\r\n        send_email('Upload Successful', f'Uploaded file: {newest_file}\\nUploaded directory: {newest_dir}')\r\n    else:\r\n        print(\"Nothing was uploaded.\")\r\n        send_email('Nothing was Uploaded', 'No new files or directories found on the first day of this month.')\r\n\r\n    return newest_file, newest_dir\r\n\r\ndef main():\r\n    # &#91;Same as before]\r\n\r\nif __name__ == \"__main__\":\r\n    main()\r\n\n<\/code><\/pre>\n\n\n\n<p>There is no error handling for email, so add this if needed. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following script uploads files from the first of the month via S3 With Notification Email The following version sends an email with the results There is no error handling for email, so add this if needed.<\/p>\n","protected":false},"author":1,"featured_media":240,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[10,3],"class_list":["post-239","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-backup","tag-backup","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 - First of the Month Files - 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=239\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python - First of the Month Files - 77 Interactive\" \/>\n<meta property=\"og:description\" content=\"The following script uploads files from the first of the month via S3 With Notification Email The following version sends an email with the results There is no error handling for email, so add this if needed.\" \/>\n<meta property=\"og:url\" content=\"http:\/\/77interactive.com\/?p=239\" \/>\n<meta property=\"og:site_name\" content=\"77 Interactive\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-12T17:09:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-02T16:56:59+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/07\/document_512.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"512\" \/>\n\t<meta property=\"og:image:height\" content=\"512\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/77interactive.com\/?p=239#article\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/?p=239\"},\"author\":{\"name\":\"Rudy\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"headline\":\"Python &#8211; First of the Month Files\",\"datePublished\":\"2023-07-12T17:09:04+00:00\",\"dateModified\":\"2023-08-02T16:56:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=239\"},\"wordCount\":44,\"image\":{\"@id\":\"http:\/\/77interactive.com\/?p=239#primaryimage\"},\"thumbnailUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/07\/document_512.jpg\",\"keywords\":[\"backup\",\"python\"],\"articleSection\":[\"backup\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/77interactive.com\/?p=239\",\"url\":\"http:\/\/77interactive.com\/?p=239\",\"name\":\"Python - First of the Month Files - 77 Interactive\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=239#primaryimage\"},\"image\":{\"@id\":\"http:\/\/77interactive.com\/?p=239#primaryimage\"},\"thumbnailUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/07\/document_512.jpg\",\"datePublished\":\"2023-07-12T17:09:04+00:00\",\"dateModified\":\"2023-08-02T16:56:59+00:00\",\"author\":{\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"breadcrumb\":{\"@id\":\"http:\/\/77interactive.com\/?p=239#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/77interactive.com\/?p=239\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/77interactive.com\/?p=239#primaryimage\",\"url\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/07\/document_512.jpg\",\"contentUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/07\/document_512.jpg\",\"width\":512,\"height\":512},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/77interactive.com\/?p=239#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/77interactive.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python &#8211; First of the Month Files\"}]},{\"@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 - First of the Month Files - 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=239","og_locale":"en_US","og_type":"article","og_title":"Python - First of the Month Files - 77 Interactive","og_description":"The following script uploads files from the first of the month via S3 With Notification Email The following version sends an email with the results There is no error handling for email, so add this if needed.","og_url":"http:\/\/77interactive.com\/?p=239","og_site_name":"77 Interactive","article_published_time":"2023-07-12T17:09:04+00:00","article_modified_time":"2023-08-02T16:56:59+00:00","og_image":[{"width":512,"height":512,"url":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/07\/document_512.jpg","type":"image\/jpeg"}],"author":"Rudy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rudy","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/77interactive.com\/?p=239#article","isPartOf":{"@id":"http:\/\/77interactive.com\/?p=239"},"author":{"name":"Rudy","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"headline":"Python &#8211; First of the Month Files","datePublished":"2023-07-12T17:09:04+00:00","dateModified":"2023-08-02T16:56:59+00:00","mainEntityOfPage":{"@id":"http:\/\/77interactive.com\/?p=239"},"wordCount":44,"image":{"@id":"http:\/\/77interactive.com\/?p=239#primaryimage"},"thumbnailUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/07\/document_512.jpg","keywords":["backup","python"],"articleSection":["backup"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/77interactive.com\/?p=239","url":"http:\/\/77interactive.com\/?p=239","name":"Python - First of the Month Files - 77 Interactive","isPartOf":{"@id":"http:\/\/77interactive.com\/#website"},"primaryImageOfPage":{"@id":"http:\/\/77interactive.com\/?p=239#primaryimage"},"image":{"@id":"http:\/\/77interactive.com\/?p=239#primaryimage"},"thumbnailUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/07\/document_512.jpg","datePublished":"2023-07-12T17:09:04+00:00","dateModified":"2023-08-02T16:56:59+00:00","author":{"@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"breadcrumb":{"@id":"http:\/\/77interactive.com\/?p=239#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/77interactive.com\/?p=239"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/77interactive.com\/?p=239#primaryimage","url":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/07\/document_512.jpg","contentUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/07\/document_512.jpg","width":512,"height":512},{"@type":"BreadcrumbList","@id":"http:\/\/77interactive.com\/?p=239#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/77interactive.com\/"},{"@type":"ListItem","position":2,"name":"Python &#8211; First of the Month Files"}]},{"@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\/239","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=239"}],"version-history":[{"count":0,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/239\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/media\/240"}],"wp:attachment":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=239"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}