{"id":18896,"date":"2022-01-15T08:58:49","date_gmt":"2022-01-15T03:28:49","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=18896"},"modified":"2026-07-13T12:38:14","modified_gmt":"2026-07-13T07:08:14","slug":"python-copy-file","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-copy-file\/","title":{"rendered":"Copy Files in Python: shutil.copyfile(), copy(), copy2(), and copytree()"},"content":{"rendered":"<p><strong>Quick answer:<\/strong> Use shutil.copyfile() when you need file contents at an exact destination, shutil.copy() when the destination may be a directory and permission mode matters, or shutil.copy2() when you want to attempt to preserve metadata.<\/p>\n<figure class=\"pythonpool-article-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-copy-file-strategy.png\" alt=\"Python file copy diagram comparing shutil copyfile copy copy2 and copytree with destination metadata links and backup policies\" width=\"1536\" height=\"1024\" loading=\"lazy\" decoding=\"async\"><figcaption>Pick the smallest shutil function that matches the destination and preservation contract.<\/figcaption><\/figure>\n<p>The safest standard way to copy a file in Python is the <code>shutil<\/code> module. It handles the byte transfer for you and gives you separate functions for copying file content, permission bits, and metadata.<\/p>\n<p>The main references are the official <a href=\"https:\/\/docs.python.org\/3\/library\/shutil.html\">shutil documentation<\/a>, <a href=\"https:\/\/docs.python.org\/3\/library\/pathlib.html\">pathlib documentation<\/a>, and <a href=\"https:\/\/docs.python.org\/3\/library\/os.path.html\">os.path documentation<\/a>.<\/p>\n<p>Use <code>shutil.copy()<\/code> when you only need file content and permissions. Use <code>shutil.copy2()<\/code> when the destination should keep metadata such as modification time where the operating system supports it.<\/p>\n<p>Before copying, decide how your program should handle missing source files, destination folders that do not exist, and existing files with the same name. Those choices are more important than the one-line copy call.<\/p>\n<p>Also decide whether you are copying a single file, a group of matching files, or an entire folder tree. A file copy and a directory copy use different functions. Keeping those cases separate makes the script easier to test and safer to run more than once.<\/p>\n<p>Use absolute paths for scheduled jobs and automation, or build paths from a known project root. Scripts launched from cron, system services, or task runners may not start in the folder you expect.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#Copy_One_File\" >Copy One File<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#Preserve_Metadata_With_copy2\" >Preserve Metadata With copy2<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#Avoid_Accidental_Overwrites\" >Avoid Accidental Overwrites<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#Copy_Many_Files_From_A_Folder\" >Copy Many Files From A Folder<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#Copy_Binary_Files_The_Same_Way\" >Copy Binary Files The Same Way<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#Handle_Copy_Errors\" >Handle Copy Errors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#Choose_The_Copy_Contract\" >Choose The Copy Contract<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#Protect_The_Destination\" >Protect The Destination<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#Frequently_Asked_Questions\" >Frequently Asked Questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#How_do_I_copy_a_file_in_Python\" >How do I copy a file in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#What_is_the_difference_between_copy_and_copy2\" >What is the difference between copy() and copy2()?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#How_do_I_copy_a_directory_in_Python\" >How do I copy a directory in Python?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/#Does_copying_a_file_create_a_backup\" >Does copying a file create a backup?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Copy_One_File\"><\/span>Copy One File<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>shutil.copy()<\/code> copies the source file to a destination file or destination folder.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from pathlib import Path\nimport shutil\n\nsource_path = Path(\"reports\/summary.txt\")\ntarget_path = Path(\"backups\/summary.txt\")\n\ntarget_path.parent.mkdir(parents=True, exist_ok=True)\nshutil.copy(source_path, target_path)\n\nprint(target_path.exists())\n<\/code><\/pre>\n<\/div>\n<p>If the second path is an existing folder, Python keeps the original file name inside that folder. If the second path is a file path, Python writes to that exact path.<\/p>\n<p>This example creates the target folder first. Without that step, the copy fails when the parent folder is missing.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Preserve_Metadata_With_copy2\"><\/span>Preserve Metadata With copy2<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>copy2()<\/code> is usually the better default for backups because it tries to preserve metadata in addition to content and permissions.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from pathlib import Path\nimport shutil\n\nsource_path = Path(\"notes\/today.txt\")\ntarget_path = Path(\"archive\/today.txt\")\n\ntarget_path.parent.mkdir(parents=True, exist_ok=True)\nresult_path = shutil.copy2(source_path, target_path)\n\nprint(result_path)\n<\/code><\/pre>\n<\/div>\n<p>The returned value is the path to the copied file. You can log it, store it, or use it in a later step.<\/p>\n<p>Metadata support differs by platform. Treat metadata preservation as best effort, not as a portable security or audit guarantee.<\/p>\n<p><!-- Python Pool visual layout repair 2026-07-13 --><\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/copy-file-source-b187.png\" alt=\"Python Pool infographic showing a source file, destination path, bytes, metadata, and shutil copy\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Source file: A source file, destination path, bytes, metadata, and shutil copy.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Avoid_Accidental_Overwrites\"><\/span>Avoid Accidental Overwrites<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>By default, copy functions replace an existing destination file. Add an explicit guard when overwriting would be dangerous.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from pathlib import Path\nimport shutil\n\nsource_path = Path(\"input\/data.csv\")\ntarget_path = Path(\"output\/data.csv\")\n\nif target_path.exists():\n    raise FileExistsError(f\"Refusing to replace {target_path}\")\n\ntarget_path.parent.mkdir(parents=True, exist_ok=True)\nshutil.copy2(source_path, target_path)\n<\/code><\/pre>\n<\/div>\n<p>This guard is simple and readable. It also gives you a single place to change behavior later, such as adding a timestamp or choosing a new file name.<\/p>\n<p>For command-line tools, print a clear message before raising or returning an error code. Silent overwrites are hard to diagnose.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/copy-file-methods-b187.png\" alt=\"Python Pool infographic comparing copyfile, copy, copy2, and their metadata behavior\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Copy methods: Copyfile, copy, copy2, and their metadata behavior.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Copy_Many_Files_From_A_Folder\"><\/span>Copy Many Files From A Folder<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code>Path.glob()<\/code> to find files that match a pattern, then copy each file into the destination folder.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from pathlib import Path\nimport shutil\n\nsource_dir = Path(\"exports\")\ntarget_dir = Path(\"backups\/exports\")\ntarget_dir.mkdir(parents=True, exist_ok=True)\n\nfor source_path in source_dir.glob(\"*.csv\"):\n    target_path = target_dir \/ source_path.name\n    shutil.copy2(source_path, target_path)\n    print(f\"Copied {source_path.name}\")\n<\/code><\/pre>\n<\/div>\n<p>This copies only files ending in <code>.csv<\/code>. Change the glob pattern for text files, logs, or other extensions.<\/p>\n<p>If the folder tree has nested subfolders, use <code>rglob()<\/code> and recreate the relative folders in the target location.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Copy_Binary_Files_The_Same_Way\"><\/span>Copy Binary Files The Same Way<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><code>shutil<\/code> copies bytes, so it works for text files, archives, database exports, and other binary files.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from pathlib import Path\nimport shutil\n\nsource_path = Path(\"build\/app.zip\")\ntarget_path = Path(\"releases\/app.zip\")\n\ntarget_path.parent.mkdir(parents=True, exist_ok=True)\nshutil.copyfile(source_path, target_path)\n\nprint(target_path.stat().st_size)\n<\/code><\/pre>\n<\/div>\n<p><code>copyfile()<\/code> requires both arguments to be file paths. It does not accept a destination folder by itself.<\/p>\n<p>Use <code>copyfile()<\/code> when you want a strict file-to-file copy. Use <code>copy()<\/code> or <code>copy2()<\/code> when a destination folder should also be accepted.<\/p>\n<p>For symbolic links, read the <code>shutil<\/code> options carefully. Some functions can copy link targets, while others can work with the link itself when the platform supports it. Pick the behavior deliberately instead of relying on defaults.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/copy-file-tree-b187.png\" alt=\"Python Pool infographic mapping a directory tree through copytree to a destination tree\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>Copy tree: A directory tree through copytree to a destination tree.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Handle_Copy_Errors\"><\/span>Handle Copy Errors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>File operations can fail because of missing paths, permissions, locked files, or full disks. Catch only the errors you can handle.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from pathlib import Path\nimport shutil\n\nsource_path = Path(\"reports\/monthly.txt\")\ntarget_path = Path(\"backups\/monthly.txt\")\n\ntry:\n    target_path.parent.mkdir(parents=True, exist_ok=True)\n    shutil.copy2(source_path, target_path)\nexcept FileNotFoundError as exc:\n    print(f\"Missing path: {exc.filename}\")\nexcept PermissionError as exc:\n    print(f\"Permission denied: {exc.filename}\")\n<\/code><\/pre>\n<\/div>\n<p>Let unexpected errors surface during development. Broad exception handling can hide real file-system problems and make data loss harder to detect.<\/p>\n<p>The practical default is <code>copy2()<\/code> with <code>pathlib.Path<\/code>, a parent-folder creation step, and a clear overwrite policy. That gives you readable code and predictable behavior across common scripts.<\/p>\n<p>When copying user-supplied paths, validate the allowed source and destination roots before running the copy. That prevents a helper script from writing outside the folder it is meant to manage.<\/p>\n<p>For large files, avoid reading the whole file into memory just to write it elsewhere. The <code>shutil<\/code> functions already stream data in chunks and may use faster platform-specific copy operations internally.<\/p>\n<p>After a copy completes, verify what matters for your use case. A backup script might check file size and timestamp. A data pipeline might check that the destination file exists before moving to the next step. A deployment helper might also compare a checksum. For copying across hosts rather than within one filesystem, <a href=\"https:\/\/www.pythonpool.com\/python-scp\/\">Python SCP File Transfer Guide<\/a> adds SSH authentication and SCP transfer handling.<\/p>\n<figure class=\"pythonpool-article-visual pythonpool-supporting-visual\"><img src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/copy-file-check-b187.png\" alt=\"Python Pool infographic testing permissions, overwrite, symlinks, paths, and validation\" width=\"1536\" height=\"1054\" loading=\"lazy\" decoding=\"async\"><figcaption>File checks: Permissions, overwrite, symlinks, paths, and validation.<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"Choose_The_Copy_Contract\"><\/span>Choose The Copy Contract<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>File copying has three separate concerns: bytes, destination naming, and metadata. <code>copyfile()<\/code> requires the complete destination filename and replaces an existing file. <code>copy()<\/code> accepts a directory and copies the permission mode. <code>copy2()<\/code> attempts to preserve more metadata, but no function can preserve every owner, ACL, resource fork, or platform-specific attribute everywhere.<\/p>\n<div class=\"pythonpool-code-scroll\" style=\"max-width:100%;overflow-x:auto;-webkit-overflow-scrolling:touch;\">\n<pre><code class=\"language-python\">from pathlib import Path\nimport shutil\n\nsource = Path(\"input\/report.csv\")\ntarget = Path(\"archive\/report.csv\")\ntarget.parent.mkdir(parents=True, exist_ok=True)\nshutil.copy2(source, target)\n<\/code><\/pre>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Protect_The_Destination\"><\/span>Protect The Destination<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Decide whether overwriting is allowed before calling a copy function. For an important artifact, copy to a temporary name, flush or close the writer that created it, then replace the final destination according to the application&#8217;s atomicity requirements. Check that the source is a regular file and that source and destination are not the same file.<\/p>\n<p>When copying a directory tree, use copytree() with an explicit destination and ignore policy. Do not assume a copy is a backup until permissions, links, timestamps, and restore behavior have been tested on the target platform.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Frequently_Asked_Questions\"><\/span>Frequently Asked Questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_copy_a_file_in_Python\"><\/span>How do I copy a file in Python?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Use shutil.copyfile() for an exact file destination, or shutil.copy() when the destination may be a directory and permission mode should be copied.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_copy_and_copy2\"><\/span>What is the difference between copy() and copy2()?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>copy() copies file contents and permission mode, while copy2() attempts to preserve additional metadata such as timestamps.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_do_I_copy_a_directory_in_Python\"><\/span>How do I copy a directory in Python?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Use shutil.copytree() with a destination directory and explicit policies for existing destinations, symlinks, and ignored files.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Does_copying_a_file_create_a_backup\"><\/span>Does copying a file create a backup?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Not automatically. A backup process should define retention, permissions, link handling, integrity checks, and a tested restore procedure.<\/p>\n<p><script type=\"application\/ld+json\">{\"@context\":\"https:\/\/schema.org\",\"@type\":\"FAQPage\",\"mainEntity\":[{\"@type\":\"Question\",\"name\":\"How do I copy a file in Python?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use shutil.copyfile() for an exact file destination, or shutil.copy() when the destination may be a directory and permission mode should be copied.\"}},{\"@type\":\"Question\",\"name\":\"What is the difference between copy() and copy2()?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"copy() copies file contents and permission mode, while copy2() attempts to preserve additional metadata such as timestamps.\"}},{\"@type\":\"Question\",\"name\":\"How do I copy a directory in Python?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Use shutil.copytree() with a destination directory and explicit policies for existing destinations, symlinks, and ignored files.\"}},{\"@type\":\"Question\",\"name\":\"Does copying a file create a backup?\",\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Not automatically. A backup process should define retention, permissions, link handling, integrity checks, and a tested restore procedure.\"}}]}<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Copy Python files with the right shutil function, destination policy, metadata expectations, symlink behavior, and backup verification.<\/p>\n","protected":false},"author":25,"featured_media":33811,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[15],"tags":[4667,1294,4672,4669,4674,4673,4676,1281,1280,4666,4671,4675,4670,4668,3680,3684],"class_list":["post-18896","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-copy-a-file-in-python","tag-copy-and-rename-file-python","tag-how-to-copy-a-file-in-python","tag-os-copy-file-python","tag-python-3-copy-file","tag-python-copy-a-file","tag-python-copy-and-paste-file","tag-python-copy-and-rename-file","tag-python-copy-file-and-rename","tag-python-copy-file-to-another-directory","tag-python-copy-file-to-directory","tag-python-copy-paste-file","tag-python-os-copy-file","tag-python-os-copy-file-and-rename","tag-python-shutil-copy","tag-shutil-copy-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v28.0) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Copy Files in Python: shutil.copyfile(), copy(), copy2(), and copytree()<\/title>\n<meta name=\"description\" content=\"Copy Python files with shutil.copyfile(), copy(), copy2(), and copytree(), while choosing paths, metadata, overwrite, symlink, and backup policies.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-copy-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Copy Files in Python: shutil.copyfile(), copy(), copy2(), and copytree()\" \/>\n<meta property=\"og:description\" content=\"Copy Python files with the right shutil function, destination policy, metadata expectations, symlink behavior, and backup verification.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-copy-file\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2022-01-15T03:28:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-07-13T07:08:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-copy-file-strategy.png\" \/>\n<meta name=\"author\" content=\"Rishav Raj\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Python Pool\" \/>\n<meta name=\"twitter:description\" content=\"Practical Python tutorials, error fixes, code examples, and project guides.\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-copy-file-strategy.png\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rishav Raj\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/\"},\"author\":{\"name\":\"Rishav Raj\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/025222e28182ecbb97e17f9f1bf15ac4\"},\"headline\":\"Copy Files in Python: shutil.copyfile(), copy(), copy2(), and copytree()\",\"datePublished\":\"2022-01-15T03:28:49+00:00\",\"dateModified\":\"2026-07-13T07:08:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/\"},\"wordCount\":1083,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-copy-file-shutil-guide-pythonpool.png\",\"keywords\":[\"copy a file in python\",\"copy and rename file python\",\"how to copy a file in python\",\"os copy file python\",\"python 3 copy file\",\"python copy a file\",\"python copy and paste file\",\"python copy and rename file\",\"python copy file and rename\",\"python copy file to another directory\",\"python copy file to directory\",\"python copy paste file\",\"python os copy file\",\"python os copy file and rename\",\"python shutil copy\",\"shutil copy python\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/\",\"name\":\"Copy Files in Python: shutil.copyfile(), copy(), copy2(), and copytree()\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-copy-file-shutil-guide-pythonpool.png\",\"datePublished\":\"2022-01-15T03:28:49+00:00\",\"dateModified\":\"2026-07-13T07:08:14+00:00\",\"description\":\"Copy Python files with shutil.copyfile(), copy(), copy2(), and copytree(), while choosing paths, metadata, overwrite, symlink, and backup policies.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-copy-file-shutil-guide-pythonpool.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2026\\\/07\\\/python-copy-file-shutil-guide-pythonpool.png\",\"width\":1350,\"height\":650,\"caption\":\"Copy a file in Python with shutil\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/python-copy-file\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pythonpool.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Copy Files in Python: shutil.copyfile(), copy(), copy2(), and copytree()\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"name\":\"Python Pool\",\"description\":\"Practical Python tutorials, error fixes, code examples, and project guides.\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pythonpool.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"contentUrl\":\"https:\\\/\\\/www.pythonpool.com\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/pythonpool\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.pythonpool.com\\\/#\\\/schema\\\/person\\\/025222e28182ecbb97e17f9f1bf15ac4\",\"name\":\"Rishav Raj\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g\",\"caption\":\"Rishav Raj\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Copy Files in Python: shutil.copyfile(), copy(), copy2(), and copytree()","description":"Copy Python files with shutil.copyfile(), copy(), copy2(), and copytree(), while choosing paths, metadata, overwrite, symlink, and backup policies.","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":"https:\/\/www.pythonpool.com\/python-copy-file\/","og_locale":"en_US","og_type":"article","og_title":"Copy Files in Python: shutil.copyfile(), copy(), copy2(), and copytree()","og_description":"Copy Python files with the right shutil function, destination policy, metadata expectations, symlink behavior, and backup verification.","og_url":"https:\/\/www.pythonpool.com\/python-copy-file\/","og_site_name":"Python Pool","article_published_time":"2022-01-15T03:28:49+00:00","article_modified_time":"2026-07-13T07:08:14+00:00","og_image":[{"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-copy-file-strategy.png","type":"","width":"","height":""}],"author":"Rishav Raj","twitter_card":"summary_large_image","twitter_title":"Python Pool","twitter_description":"Practical Python tutorials, error fixes, code examples, and project guides.","twitter_image":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-copy-file-strategy.png","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Rishav Raj","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-copy-file\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-copy-file\/"},"author":{"name":"Rishav Raj","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/025222e28182ecbb97e17f9f1bf15ac4"},"headline":"Copy Files in Python: shutil.copyfile(), copy(), copy2(), and copytree()","datePublished":"2022-01-15T03:28:49+00:00","dateModified":"2026-07-13T07:08:14+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-copy-file\/"},"wordCount":1083,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-copy-file\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-copy-file-shutil-guide-pythonpool.png","keywords":["copy a file in python","copy and rename file python","how to copy a file in python","os copy file python","python 3 copy file","python copy a file","python copy and paste file","python copy and rename file","python copy file and rename","python copy file to another directory","python copy file to directory","python copy paste file","python os copy file","python os copy file and rename","python shutil copy","shutil copy python"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-copy-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-copy-file\/","url":"https:\/\/www.pythonpool.com\/python-copy-file\/","name":"Copy Files in Python: shutil.copyfile(), copy(), copy2(), and copytree()","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-copy-file\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-copy-file\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-copy-file-shutil-guide-pythonpool.png","datePublished":"2022-01-15T03:28:49+00:00","dateModified":"2026-07-13T07:08:14+00:00","description":"Copy Python files with shutil.copyfile(), copy(), copy2(), and copytree(), while choosing paths, metadata, overwrite, symlink, and backup policies.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-copy-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-copy-file\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-copy-file\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-copy-file-shutil-guide-pythonpool.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2026\/07\/python-copy-file-shutil-guide-pythonpool.png","width":1350,"height":650,"caption":"Copy a file in Python with shutil"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-copy-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Copy Files in Python: shutil.copyfile(), copy(), copy2(), and copytree()"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Practical Python tutorials, error fixes, code examples, and project guides.","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/025222e28182ecbb97e17f9f1bf15ac4","name":"Rishav Raj","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g","caption":"Rishav Raj"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/18896","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=18896"}],"version-history":[{"count":32,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/18896\/revisions"}],"predecessor-version":[{"id":41811,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/18896\/revisions\/41811"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/33811"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=18896"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=18896"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=18896"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}