{"id":9701,"date":"2021-03-05T10:00:28","date_gmt":"2021-03-05T04:30:28","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=9701"},"modified":"2024-01-03T11:53:31","modified_gmt":"2024-01-03T06:23:31","slug":"python-shutil","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-shutil\/","title":{"rendered":"Python Shutil Module: 10 Methods You Should Know"},"content":{"rendered":"\n<p>Firstly, the <strong>Python Shutil module<\/strong>\u00a0in Python provides many functions to perform high-level operations on files and collections of files. Secondly, It is an inbuilt module that comes with the automation process of copying and removing files and directories. Thirdly, this module also takes care of low-level semantics\u00a0like creating closing files once they are copied and focusing on the business logic.  <\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 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-shutil\/#How_does_the_Python_shutil_module_work\" >How does the Python shutil module work?<\/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-shutil\/#File-Directory_operations\" >File-Directory operations<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#1_Python_shutilcopy\" >1. Python shutil.copy()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#2_Python_shutilcopy2\" >2. Python shutil.copy2()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#3_Python_shutilcopyfile\" >3. Python shutil.copyfile()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#4_Python_shutilcopytree\" >4. Python shutil.copytree()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#5_Python_shutilrmtree\" >5. Python shutil.rmtree()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#6_shutilwhich\" >6. shutil.which()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#7_Python_shutildisk_usage\" >7. Python shutil.disk_usage()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#8_Python_shutilmove\" >8. Python shutil.move()<\/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-shutil\/#9_Python_shutilmake_archive\" >9.  Python shutil.make_archive()<\/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-shutil\/#10_Python_shutilget_archive_formats\" >10. Python shutil.get_archive_formats()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#Advantages\" >Advantages<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#Must_Read\" >Must Read<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-how-does-the-python-shutil-module-work\"><span class=\"ez-toc-section\" id=\"How_does_the_Python_shutil_module_work\"><\/span>How does the Python shutil module work?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The basic syntax to use shutil module is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import shutil\nshutil.submodule_name(arguments)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-file-directory-operations\"><span class=\"ez-toc-section\" id=\"File-Directory_operations\"><\/span>File-Directory operations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-python-shutil-copy\"><span class=\"ez-toc-section\" id=\"1_Python_shutilcopy\"><\/span>1. Python shutil.copy()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>shutil.copy()<\/strong>: This function is used to copy the content or text of the source file to the destination file or directories. It also preserves the file\u2019s permission mode, but another type of metadata of the file, like the file\u2019s creation and modification, is not preserved.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os \n  \n# import the shutil module  \nimport shutil \n  \n# write the path of the file\npath = '\/home\/User'\n  \n# List all the files and directories in the given path\nprint(&quot;Before copying file:&quot;) \nprint(os.listdir(path)) \n  \n  \n# write the Source path \nsource = &quot;\/home\/User\/file.txt&quot;\n  \n# Print the file permission of the source given\nperms = os.stat(source).st_mode \nprint(&quot;File Permission mode:&quot;, perms, &quot;\\n&quot;) \n  \n# Write the Destination path \ndestinationfile = &quot;\/home\/User\/file(copy).txt&quot;\n  \n# Copy the content of source file to destination file \ndests = shutil.copy(source, destinationfile) \n  \n# List files and directories of the path \nprint(&quot;After copying file:&quot;) \nprint(os.listdir(path)) \n  \n# Print again all the file permission\nperms = os.stat(destinationfile).st_mode \nprint(&quot;File Permission mode:&quot;, perms) \n  \n# Print path of of the file which is created\nprint(&quot;Destination path:&quot;, dests) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Before copying file:\n&#91;'hrithik.png', 'test.py', 'file.text', 'copy.cpp']\nFile permission mode: 33188\n\nAfter copying file:\n&#91;'hrithik.png', 'test.py',  'file.text', 'file(copy).txt', 'copy.cpp']\nFile permission mode: 33188 \nDestination path: \/home\/User\/file(copy).txt<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation\"><strong>Explanation:<\/strong> <\/h4>\n\n\n\n<p>Firstly, In this code, we are checking with the files in the directory. Secondly, then we will print the file permissions and give the file&#8217;s source path. Thirdly, we will give the destination path a copy of the content there in a new file. At last, we will again print all the files in the directory and check if a copy of that file was created or not.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-python-shutil-copy2\"><span class=\"ez-toc-section\" id=\"2_Python_shutilcopy2\"><\/span>2. Python shutil.copy2()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Firstly, this function is just like the copy() function except for the fact that it maintains the metadata of the source file.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom shutil import *\nimport os\nimport time\nimport sys\n\ndef show_file_info(filename):\n    stat_info = os.stat(filename)\n    print '\\tMode    :', stat_info.st_mode\n    print '\\tCreated :', time.ctime(stat_info.st_ctime)\n    print '\\tAccessed:', time.ctime(stat_info.st_atime)\n    print '\\tModified:', time.ctime(stat_info.st_mtime)\n\nos.mkdir('example')\nprint ('SOURCE time: ')\nshow_file_info('shutil_copy2.py')\ncopy2('shutil_copy2.py', 'example')\nprint ('DESTINATION time:')\nshow_file_info('example\/shutil_copy2.py')\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SOURCE time:\n        Mode    : 33188\n        Created : Sat Jul 16 12:28:43 2020\n        Accessed: Thu Feb 21 06:36:54 2021\n        Modified: Sat Feb 19 19:18:23 2021\nDESTINATION time:\n        Mode    : 33188\n        Created : Mon Mar 1 06:36:54 2021\n        Accessed: Mon Mar 1 06:36:54 2021\n        Modified: Tue Mar 2 19:18:23 2021 <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-1\"><strong>Explanation:<\/strong><\/h4>\n\n\n\n<p>In this code, we have written the function copy2() is the same as a copy, just it performs one extra operation that maintains the metadata. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-python-shutil-copyfile\"><span class=\"ez-toc-section\" id=\"3_Python_shutilcopyfile\"><\/span>3. Python shutil.copyfile()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this function file, names get copied, which means the specified name copies the original file in the same directory. It says that the duplicate of the file is present in the same directory.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nimport shutil\n\nprint('BEFORE LIST:', os.listdir('.'))\nshutil.copyfile('file_copy.py', 'file_copy.py.copy')\nprint('AFTER LIST:', os.listdir('.'))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Latracal:shutil Latracal$ python file_copy.py\nBEFORE LIST: \n&#91;' .DS_Store', 'file_copy.py']\nAFTER LIST: \n&#91; .DS_Store', 'file_copy.py', 'file_copy.py.copy']<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-2\"><strong>Explanation:<\/strong> <\/h4>\n\n\n\n<p>In this code, we have written the function <code>copyfile()<\/code> where the same file name gets copied for the new file. Just a copy is added in the new file name. See the output.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-python-shutil-copytree\"><span class=\"ez-toc-section\" id=\"4_Python_shutilcopytree\"><\/span>4. Python shutil.copytree()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This function copies the file and the <a href=\"http:\/\/www.pythonpool.com\/python-import-from-subdirectory\/\" target=\"_blank\" rel=\"noopener\">subdirectories<\/a> in one directory to another directory. That means that the file is present in the source as well as the destination. The names of both the parameters must be in the string.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pprint\nimport shutil\nimport os\n\nshutil.copytree('..\/shutil', '.\/Latracal')\npprint.pprint(os.listdir('.\/Latracal'))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Latracal:shutil Latracal$ clone\u2014directory. py\n&#91;' .DS\u2014Store' ,\n'file_copy.py' ,\n'file_copy_new.py'\n'file_with_metadata.py' , \n'clone_directory. py']<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-3\"><strong>Explanation:<\/strong><\/h4>\n\n\n\n<p>In this code, we have written the function copytree() to get a duplicate of that file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-python-shutil-rmtree\"><span class=\"ez-toc-section\" id=\"5_Python_shutilrmtree\"><\/span>5. Python shutil.rmtree()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This function removes the particular file and subdirectory from the specified directory, which means the directory is deleted from the system.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pprint\nimport shutil\nimport os\n\nprint('BEFORE:')\npprint.pprint(os.listdir('.'))\n\nshutil.rmtree('Latracal')\n\nprint('\\nAFTER:')\npprint.pprint(os.listdir('.'))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Latracal:shutil Latracal$ retove\u2014dir.py\nBEFORE:\n&#91;'.DS_Store',\n'file_copy.py',\n\n'file_copy_new.py',\n'remove_dir.py',\n\n'copy_with_metadata.py',\n'Latracal'\n'clone_directory.py']\n\n\nAFTER:\n&#91;'.DS_Store',\n'file\u2014copy.py' ,\n'file_copy_new.py',\n'remove_dir.py',\n\n'copy_with_metadata.py',\n'clone_directory. py']<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-4\"><strong>Explanation:<\/strong><\/h4>\n\n\n\n<p>In this code, we have written the function rmtree(), which removes the file or directory. Firstly, we have listed all the files and applied the function to remove them, and we have again listed the files to see if the file has been deleted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-6-shutil-which\"><span class=\"ez-toc-section\" id=\"6_shutilwhich\"><\/span>6. shutil.which()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The\u00a0<code>which()<\/code>\u00a0a function is an excellent tool that is used to find the file path in your machine to easily reach the particular destination by knowing the file&#8217;s path.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\nimport sys\n\nprint(shutil.which('bsondump'))\nprint(shutil.which('no-such-program'))\n<\/pre><\/div>\n\n\n<p><strong>output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Latracal:shutil Latracal$  python find_file.py\n\/usr\/10ca1\/mngodb@3.2\/bin\/bsondunp<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-5\"><strong>Explanation:<\/strong><\/h4>\n\n\n\n<p>In this code, we have written the function that () to find any of the files when required. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-7-python-shutil-disk-usage\"><span class=\"ez-toc-section\" id=\"7_Python_shutildisk_usage\"><\/span>7. Python shutil.disk_usage()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This function is used to understand how much information is present in our file system by just calling the disk_usage() function.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\n\ntotal_mem, used_mem, free_mem = shutil.disk_usage('.')\ngb = 10 **9\n\nprint('Total: {:6.2f} GB'.format(total_mem\/gb))\nprint('Used : {:6.2f} GB'.format(used_mem\/gb))\nprint('Free : {:6.2f} GB'.format(free_mem\/gb))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>shubhm:shutil shubhmS py\nTotal:499.9\u00f8 GB\nUsed :187.72 GB\nFree :3\u00f88.26 GB<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-6\"><strong>Explanation:<\/strong><\/h4>\n\n\n\n<p>In this code, we have written the function disk_usage() to learn about the total, used, and free disk space.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-8-python-shutil-move\"><span class=\"ez-toc-section\" id=\"8_Python_shutilmove\"><\/span>8. Python shutil.move()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This function moves the file and directory from one directory to another and removes it from the previous directory. It can be said to rename the file or directory.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\nshutil.move('hello.py','newdir\/')\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 'newdir\/hello.py'<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-7\"><strong>Explanation:<\/strong><\/h4>\n\n\n\n<p>In this code, we have written the function move() to move the file or directory from one place to another.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-9-python-shutil-make-archive\"><span class=\"ez-toc-section\" id=\"9_Python_shutilmake_archive\"><\/span>9.  Python shutil.make_archive()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This function builds an archive (zip or tar) of files in the root directory.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\nimport pprint\n\nroot_directory='newdir'\nshutil.make_archive(&quot;newdirabcd&quot;,&quot;zip&quot;,root_directory)\n<\/pre><\/div>\n\n\n<p><strong>output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'C:\\\\python\\\\latracal\\\\newdirabcd.zip' <\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-8\"><strong>Explanation:<\/strong><\/h4>\n\n\n\n<p>In this code, we have written the functionmake_archive()  with telling them the name of the root directory to build the archive of files in the root directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-10-python-shutil-get-archive-formats\"><span class=\"ez-toc-section\" id=\"10_Python_shutilget_archive_formats\"><\/span>10. Python shutil.get_archive_formats()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>This function gives us all the supported archive formats in the file or directory.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\nimport sys \n\nshutil.get_archive_formats()\n<\/pre><\/div>\n\n\n<p><strong>output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;('bztar', \"bzip2'ed tar-file\"), ('gztar', \"gzip'ed tar-file\"), ('tar', 'uncompressed tar file'), ('xztar', \"xz'ed tar-file\"), ('zip', 'ZIP file')]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-explanation-9\"><strong>Explanation:<\/strong> <\/h4>\n\n\n\n<p>In this code,  we have written the function get_archive_formats() to get the supportive archive formats in the file or directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-advantages\"><span class=\"ez-toc-section\" id=\"Advantages\"><\/span>Advantages<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The\u00a0<strong>shutil<\/strong>\u00a0module helps you automate the copying of files and directories.<\/li>\n\n\n\n<li>This module saves the steps of opening, reading, writing, and closing files when there is no actual processing; it simply moves files.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading has-luminous-vivid-orange-color has-text-color\" id=\"h-must-read\"><span class=\"ez-toc-section\" id=\"Must_Read\"><\/span>Must Read<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-yoast-seo-related-links\"><li><a href=\"http:\/\/www.pythonpool.com\/python-delete-file\/\">[Best] Ways to Delete a File in Python<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-rename-file\/\">Python Rename File With Best Practices In-depth Examples<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/dijkstras-algorithm-python\/\">Implementing Dijkstra&#8217;s Algorithm in Python<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-listdir\/\">8 Examples to Implement os.listdir() in Python<\/a><\/li><li><a href=\"http:\/\/www.pythonpool.com\/python-unzip\/\">Unzip a File in Python: 5 Scenarios You Should Know<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this article, we have studied many types of operations that show how we can work on high-level file operations like copying contents of a file and creating a new copy of a file, etc., without diving into complex File Handling operations with\u00a0<code>shutil<\/code>\u00a0module in Python.<\/p>\n\n\n\n<p>However, if you have any questions, please let me know in the comment section below. I will try to help you as soon as possible.<\/p>\n\n\n\n<p><strong><em>Happy Pythoning!<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Firstly, the Python Shutil module\u00a0in Python provides many functions to perform high-level operations on files and collections of files. Secondly, It is an inbuilt module &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Python Shutil Module: 10 Methods You Should Know\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-shutil\/#more-9701\" aria-label=\"More on Python Shutil Module: 10 Methods You Should Know\">Read more<\/a><\/p>\n","protected":false},"author":17,"featured_media":9837,"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":[3680,745,3683,3678,3682,3684,3679,3681],"class_list":["post-9701","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-python-shutil-copy","tag-python-shutil-delete-file","tag-python-shutil-module","tag-python-shutil-move","tag-python-shutil-move-overwrite","tag-shutil-copy-python","tag-shutil-python","tag-shutil-python-3","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Shutil Module: 10 Methods You Should Know - Python Pool<\/title>\n<meta name=\"description\" content=\"&quot;Python shutil module&quot; performs high-level operations on files and directory which help us to copy and remove files and directory.\" \/>\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-shutil\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Shutil Module: 10 Methods You Should Know\" \/>\n<meta property=\"og:description\" content=\"Firstly, the Python Shutil module\u00a0in Python provides many functions to perform high-level operations on files and collections of files. Secondly, It is an\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-shutil\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-03-05T04:30:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-03T06:23:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/03\/Python-Shutil-Methods-You-Should-Know.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1350\" \/>\n\t<meta property=\"og:image:height\" content=\"650\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Siddharth Jain\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\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=\"Siddharth Jain\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-shutil\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-shutil\/\"},\"author\":{\"name\":\"Siddharth Jain\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa\"},\"headline\":\"Python Shutil Module: 10 Methods You Should Know\",\"datePublished\":\"2021-03-05T04:30:28+00:00\",\"dateModified\":\"2024-01-03T06:23:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-shutil\/\"},\"wordCount\":854,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-shutil\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/03\/Python-Shutil-Methods-You-Should-Know.png\",\"keywords\":[\"python shutil copy\",\"python shutil delete file\",\"python shutil module\",\"python shutil move\",\"python shutil move overwrite\",\"shutil copy python\",\"shutil python\",\"shutil python 3\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-shutil\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/python-shutil\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-shutil\/\",\"name\":\"Python Shutil Module: 10 Methods You Should Know - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-shutil\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-shutil\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/03\/Python-Shutil-Methods-You-Should-Know.png\",\"datePublished\":\"2021-03-05T04:30:28+00:00\",\"dateModified\":\"2024-01-03T06:23:31+00:00\",\"description\":\"\\\"Python shutil module\\\" performs high-level operations on files and directory which help us to copy and remove files and directory.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-shutil\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-shutil\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-shutil\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/03\/Python-Shutil-Methods-You-Should-Know.png\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/03\/Python-Shutil-Methods-You-Should-Know.png\",\"width\":1350,\"height\":650,\"caption\":\"python shutil\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-shutil\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Shutil Module: 10 Methods You Should Know\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"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\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/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\/75a3240fabe5ea90200777a9d8d3b4fa\",\"name\":\"Siddharth Jain\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g\",\"caption\":\"Siddharth Jain\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Python Shutil Module: 10 Methods You Should Know - Python Pool","description":"\"Python shutil module\" performs high-level operations on files and directory which help us to copy and remove files and directory.","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-shutil\/","og_locale":"en_US","og_type":"article","og_title":"Python Shutil Module: 10 Methods You Should Know","og_description":"Firstly, the Python Shutil module\u00a0in Python provides many functions to perform high-level operations on files and collections of files. Secondly, It is an","og_url":"https:\/\/www.pythonpool.com\/python-shutil\/","og_site_name":"Python Pool","article_published_time":"2021-03-05T04:30:28+00:00","article_modified_time":"2024-01-03T06:23:31+00:00","og_image":[{"width":1350,"height":650,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/03\/Python-Shutil-Methods-You-Should-Know.png","type":"image\/png"}],"author":"Siddharth Jain","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Siddharth Jain","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-shutil\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-shutil\/"},"author":{"name":"Siddharth Jain","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa"},"headline":"Python Shutil Module: 10 Methods You Should Know","datePublished":"2021-03-05T04:30:28+00:00","dateModified":"2024-01-03T06:23:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-shutil\/"},"wordCount":854,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-shutil\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/03\/Python-Shutil-Methods-You-Should-Know.png","keywords":["python shutil copy","python shutil delete file","python shutil module","python shutil move","python shutil move overwrite","shutil copy python","shutil python","shutil python 3"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-shutil\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-shutil\/","url":"https:\/\/www.pythonpool.com\/python-shutil\/","name":"Python Shutil Module: 10 Methods You Should Know - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-shutil\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-shutil\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/03\/Python-Shutil-Methods-You-Should-Know.png","datePublished":"2021-03-05T04:30:28+00:00","dateModified":"2024-01-03T06:23:31+00:00","description":"\"Python shutil module\" performs high-level operations on files and directory which help us to copy and remove files and directory.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-shutil\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-shutil\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-shutil\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/03\/Python-Shutil-Methods-You-Should-Know.png","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/03\/Python-Shutil-Methods-You-Should-Know.png","width":1350,"height":650,"caption":"python shutil"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-shutil\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Python Shutil Module: 10 Methods You Should Know"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","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":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/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\/75a3240fabe5ea90200777a9d8d3b4fa","name":"Siddharth Jain","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g","caption":"Siddharth Jain"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/9701","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\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=9701"}],"version-history":[{"count":31,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/9701\/revisions"}],"predecessor-version":[{"id":31891,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/9701\/revisions\/31891"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/9837"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=9701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=9701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=9701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}