{"id":90,"date":"2023-06-09T03:02:07","date_gmt":"2023-06-09T03:02:07","guid":{"rendered":"http:\/\/77interactive.com\/?p=90"},"modified":"2023-06-22T21:53:32","modified_gmt":"2023-06-22T21:53:32","slug":"copy-directory-and-contents-in-python","status":"publish","type":"post","link":"http:\/\/77interactive.com\/?p=90","title":{"rendered":"Copy Directory and Contents in Python"},"content":{"rendered":"\n<p>The following script copies a directory and all of its contents to another directory<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import shutil\nimport os\nimport argparse\n\ndef copy_directory(src, dst):\n    \"\"\"\n    This function copies a directory, including all of its content and hidden files, to another directory.\n\n    Args:\n        src (str): The source directory.\n        dst (str): The destination directory.\n\n    Returns:\n        None\n    \"\"\"\n    # Create the destination directory path including the source directory name\n    dst = os.path.join(dst, os.path.basename(src))\n    \n    # Ensure the destination directory exists\n    os.makedirs(dst, exist_ok=True)\n\n    # Iterate over all the files in source directory\n    for item in os.listdir(src):\n        s = os.path.join(src, item)\n        d = os.path.join(dst, item)\n        \n        # If item is a directory, recurse into it\n        if os.path.isdir(s):\n            print(f\"Copying directory {s} to {d}\")\n            copy_directory(s, d)\n        else:\n            # Otherwise, copy it\n            print(f\"Copying file {s} to {d}\")\n            shutil.copy2(s, d)\n\ndef main():\n    parser = argparse.ArgumentParser(description='Copy a directory including all of its content and hidden files to another directory.')\n    parser.add_argument('src', type=str, help='The source directory')\n    parser.add_argument('dst', type=str, help='The destination directory')\n\n    args = parser.parse_args()\n\n    copy_directory(args.src, args.dst)\n\n\nif __name__ == \"__main__\":\n    main()\n\n\n<\/code><\/pre>\n\n\n\n<p>Call the script like this<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python3 copy_dir.py \/path\/to\/source\/directory \/path\/to\/destination\/directory\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The following script copies a directory and all of its contents to another directory Call the script like this<\/p>\n","protected":false},"author":1,"featured_media":226,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[3],"class_list":["post-90","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Copy Directory and Contents in Python - 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=90\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Copy Directory and Contents in Python - 77 Interactive\" \/>\n<meta property=\"og:description\" content=\"The following script copies a directory and all of its contents to another directory Call the script like this\" \/>\n<meta property=\"og:url\" content=\"http:\/\/77interactive.com\/?p=90\" \/>\n<meta property=\"og:site_name\" content=\"77 Interactive\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-09T03:02:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-22T21:53:32+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/06\/Bear001.png\" \/>\n\t<meta property=\"og:image:width\" content=\"500\" \/>\n\t<meta property=\"og:image:height\" content=\"500\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/77interactive.com\/?p=90#article\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/?p=90\"},\"author\":{\"name\":\"Rudy\",\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"headline\":\"Copy Directory and Contents in Python\",\"datePublished\":\"2023-06-09T03:02:07+00:00\",\"dateModified\":\"2023-06-22T21:53:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=90\"},\"wordCount\":25,\"image\":{\"@id\":\"http:\/\/77interactive.com\/?p=90#primaryimage\"},\"thumbnailUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/06\/Bear001.png\",\"keywords\":[\"python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/77interactive.com\/?p=90\",\"url\":\"http:\/\/77interactive.com\/?p=90\",\"name\":\"Copy Directory and Contents in Python - 77 Interactive\",\"isPartOf\":{\"@id\":\"http:\/\/77interactive.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/77interactive.com\/?p=90#primaryimage\"},\"image\":{\"@id\":\"http:\/\/77interactive.com\/?p=90#primaryimage\"},\"thumbnailUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/06\/Bear001.png\",\"datePublished\":\"2023-06-09T03:02:07+00:00\",\"dateModified\":\"2023-06-22T21:53:32+00:00\",\"author\":{\"@id\":\"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9\"},\"breadcrumb\":{\"@id\":\"http:\/\/77interactive.com\/?p=90#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/77interactive.com\/?p=90\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/77interactive.com\/?p=90#primaryimage\",\"url\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/06\/Bear001.png\",\"contentUrl\":\"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/06\/Bear001.png\",\"width\":500,\"height\":500},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/77interactive.com\/?p=90#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/77interactive.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Copy Directory and Contents in Python\"}]},{\"@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":"Copy Directory and Contents in Python - 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=90","og_locale":"en_US","og_type":"article","og_title":"Copy Directory and Contents in Python - 77 Interactive","og_description":"The following script copies a directory and all of its contents to another directory Call the script like this","og_url":"http:\/\/77interactive.com\/?p=90","og_site_name":"77 Interactive","article_published_time":"2023-06-09T03:02:07+00:00","article_modified_time":"2023-06-22T21:53:32+00:00","og_image":[{"width":500,"height":500,"url":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/06\/Bear001.png","type":"image\/png"}],"author":"Rudy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rudy","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/77interactive.com\/?p=90#article","isPartOf":{"@id":"http:\/\/77interactive.com\/?p=90"},"author":{"name":"Rudy","@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"headline":"Copy Directory and Contents in Python","datePublished":"2023-06-09T03:02:07+00:00","dateModified":"2023-06-22T21:53:32+00:00","mainEntityOfPage":{"@id":"http:\/\/77interactive.com\/?p=90"},"wordCount":25,"image":{"@id":"http:\/\/77interactive.com\/?p=90#primaryimage"},"thumbnailUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/06\/Bear001.png","keywords":["python"],"articleSection":["Python"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/77interactive.com\/?p=90","url":"http:\/\/77interactive.com\/?p=90","name":"Copy Directory and Contents in Python - 77 Interactive","isPartOf":{"@id":"http:\/\/77interactive.com\/#website"},"primaryImageOfPage":{"@id":"http:\/\/77interactive.com\/?p=90#primaryimage"},"image":{"@id":"http:\/\/77interactive.com\/?p=90#primaryimage"},"thumbnailUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/06\/Bear001.png","datePublished":"2023-06-09T03:02:07+00:00","dateModified":"2023-06-22T21:53:32+00:00","author":{"@id":"http:\/\/77interactive.com\/#\/schema\/person\/0e61d2a984b8304618026b207e6121e9"},"breadcrumb":{"@id":"http:\/\/77interactive.com\/?p=90#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/77interactive.com\/?p=90"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/77interactive.com\/?p=90#primaryimage","url":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/06\/Bear001.png","contentUrl":"http:\/\/77interactive.com\/wp-content\/uploads\/2023\/06\/Bear001.png","width":500,"height":500},{"@type":"BreadcrumbList","@id":"http:\/\/77interactive.com\/?p=90#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/77interactive.com\/"},{"@type":"ListItem","position":2,"name":"Copy Directory and Contents in Python"}]},{"@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\/90","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=90"}],"version-history":[{"count":0,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/posts\/90\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=\/wp\/v2\/media\/226"}],"wp:attachment":[{"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=90"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=90"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/77interactive.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=90"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}