{"id":11261,"date":"2020-12-07T15:02:07","date_gmt":"2020-12-07T15:02:07","guid":{"rendered":"https:\/\/www.askpython.com\/?p=11261"},"modified":"2023-02-16T19:56:57","modified_gmt":"2023-02-16T19:56:57","slug":"shutil-module","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/shutil-module","title":{"rendered":"Shutil Module in Python"},"content":{"rendered":"\n<p>Hey! In this tutorial, we will learn about the functions present in the shutil module of Python. So, let&#8217;s get started.<\/p>\n\n\n\n<p>Python&#8217;s shutil module provides us a number of high-level operations on files. We can copy and remove files and directories. Let&#8217;s get started with the module and learn the practical implementation of each of the files in detail.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to copy files with shutil module?<\/h2>\n\n\n\n<p>There are various methods available in shutil module to copy the contents  of one file to another file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. shutil.copyfileobj(src,dst)<\/h3>\n\n\n\n<p>Suppose, we want to copy the contents of the file <em>data.txt<\/em> to <em>data1.txt<\/em>, we can use the follow piece of code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\n\nf=open(&#039;data.txt&#039;,&#039;r&#039;)\nf1=open(&#039;data1.txt&#039;,&#039;w&#039;)\n\n# Syntax: shutil.copyfileobj(src,dst)\nshutil.copyfileobj(f,f1)\n\nf.close()\nf1.close()\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\"><strong>2. shutil.copy(src,dst)<\/strong><\/h3>\n\n\n\n<p>Another method of copying the data of one file to another can be without creating the file object. Here, we pass the relative path of our files.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\n#shutil.copy(src.dst)\nshutil.copy(&#039;data.txt&#039;,&#039;data1.txt&#039;)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. shutil.copy2(src,dst)<\/h3>\n\n\n\n<p><em>copy<\/em>(src,dst) and <em>copy2(src,dst)<\/em> functions are almost the same but <em>copy2(src,dst)<\/em> also copies the metadata of the source file. <\/p>\n\n\n\n<p>Metadata includes the information about when the file was created, accessed or modified.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\n#shutil.copy2(src,dst)\nshutil.copy2(&#039;data.txt&#039;,&#039;data1.txt&#039;)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">4. shutil.copyfile(src,dst)<\/h3>\n\n\n\n<p>Here, source and destination can be relative path or absolute path. Suppose, we want to copy a file to a folder, we can use the following code snippet:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\nimport os\n\npath=&#039;D:\\DSCracker\\DS Cracker\\Python&#039;\nprint(&quot;Before copying file:&quot;) \nprint(os.listdir(path)) \n\nshutil.copyfile(&#039;data.txt&#039;,&#039;Python\/data3.txt&#039;)\n\nprint(&quot;After copying file:&quot;) \nprint(os.listdir(path))\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nBefore copying file:\n&#x5B;&#039;hey.py&#039;]\nAfter copying file:\n&#x5B;&#039;data3.txt&#039;, &#039;hey.py&#039;]\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5. shutil.move(src,dst)<\/h3>\n\n\n\n<p>Suppose, we want to delete a file from one location and move it to another location. Here, let&#8217;s move <em>shutil.py<\/em> from source to a different location:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\nimport os\n\npath=&#039;D:\\DSCracker\\DS Cracker&#039;\nprint(&quot;Source folder:&quot;) \nprint(os.listdir(path))\n\npath1=&#039;D:\\DSCracker\\DS Cracker\\Python&#039;\nshutil.move(&#039;shutil.py&#039;,&#039;Python&#039;)\n\nprint(&quot;After moving file shutil.py to destination folder, destination contains:&quot;) \nprint(os.listdir(path1))\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSource folder:\n&#x5B;&#039;cs&#039;, &#039;data.txt&#039;, &#039;Python&#039;, &#039;ReverseArray&#039;, &#039;ReverseArray.cpp&#039;, &#039;shutil.py&#039;]\nAfter moving file shutill.py to destination folder, destination contains:\n&#x5B;&#039;data1.txt&#039;, &#039;data3.txt&#039;, &#039;hey.py&#039;, &#039;nsawk.py&#039;, &#039;shutil.py&#039;]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">6. shutil.copytree(src,dst)<\/h3>\n\n\n\n<p>If we want to copy a complete folder which includes all its files to a new location, we can use <em>copytree(src,dst<\/em>) function.<\/p>\n\n\n\n<p>It recursively copies an entire directory tree rooted at <em>src<\/em> to directory named <em>dst<\/em> and returns the destination directory. <\/p>\n\n\n\n<p>Let&#8217;s copy the folder <em>Python<\/em> to the folder <em>Newfolder<\/em>.  <\/p>\n\n\n\n<p><strong>Note:<\/strong> We have to create a new folder inside our destination folder as the function does not allow copying contents to an already existing folder.<\/p>\n\n\n\n<p>So here, we have created the folder <em>python1<\/em> inside the folder <em>Newfolder<\/em>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nimport shutil\n\npath=&#039;D:\\DSCracker\\DS Cracker\\Python&#039;\nprint(&quot;Source folder:&quot;) \nprint(os.listdir(path))\n\nshutil.copytree(&#039;Python&#039;,&#039;NewPython\/python1&#039;)\n\npath1=&#039;D:\\DSCracker\\DS Cracker\\NewPython\\python1&#039;\nprint(&quot;Destination folder:&quot;)\nprint(os.listdir(path1))\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSource folder:\n&#x5B;&#039;data1.txt&#039;, &#039;data3.txt&#039;, &#039;hey.py&#039;, &#039;nsawk.py&#039;, &#039;shutill.py&#039;]\nDestination folder:\n&#x5B;&#039;data1.txt&#039;, &#039;data3.txt&#039;, &#039;hey.py&#039;, &#039;nsawk.py&#039;, &#039;shutill.py&#039;]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">How to remove\/delete files with shutil module?<\/h2>\n\n\n\n<p>Now that we&#8217;ve learned how we can work with the moving and copying files, let&#8217;s learn to remove or delete files from specific locations from within your Python scripts.<\/p>\n\n\n\n<p><strong>By using shutil.rmtree()<\/strong>,  we can delete any folder,file or directory. Let&#8217;s delete the folder <em>Python<\/em>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport os\nimport shutil\n\npath=&#039;D:\\DSCracker\\DS Cracker&#039;\nprint(&quot;Before deleting:&quot;) \nprint(os.listdir(path))\n\nshutil.rmtree(&#039;Python&#039;)\nprint(&quot;After deleting:&quot;) \nprint(os.listdir(path))\n\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nBefore deleting:\n&#x5B;&#039;cs&#039;, &#039;data.txt&#039;, &#039;NewPython&#039;, &#039;program.py&#039;, &#039;Python&#039;, &#039;ReverseArray&#039;, &#039;ReverseArray.cpp&#039;]\n\nAfter deleting:\n&#x5B;&#039;cs&#039;, &#039;data.txt&#039;, &#039;NewPython&#039;, &#039;program.py&#039;, &#039;ReverseArray&#039;, &#039;ReverseArray.cpp&#039;]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">How to copy permission bits of one file to another?<\/h2>\n\n\n\n<p>Copying file is one part. What if you just want to copy the same permissions of a file to all other files? Let&#8217;s learn to do just that using the shutil module here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. shutil.copymode(src,dst)<\/h3>\n\n\n\n<p>This method copies the permission bits from src to dst. Let&#8217;s copy permission bits of <em>Python<\/em> directory to <em>Python1<\/em> directory.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\nimport os\nsrc= &#039;D:\\\\DSCracker\\\\DS Cracker\\\\Python&#039;\ndest=&#039;D:\\\\DSCracker\\\\DS Cracker\\\\Python1&#039;\n\nprint(&quot;Before using shutil.copymode(), Permission bits of destination:&quot;)\nprint(oct(os.stat(dest).st_mode)&#x5B;-3:])\n\nshutil.copymode(src, dest) \nprint(&quot;After using shutil.copymode(), Permission bit of destination:&quot;)\nprint(oct(os.stat(dest).st_mode)&#x5B;-3:])\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nBefore using shutil.copymode(), Permission bits of source:\n677\nAfter using shutil.copymode(), Permission bit of destination:\n777\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. shutil.copystat(src,dst)<\/h3>\n\n\n\n<p>shutil.copystat(src.dst) copies permission bits along with metadata.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\nimport os\nimport time \n\nsrc= &#039;D:\\\\DSCracker\\\\DS Cracker\\\\Python&#039;\ndest=&#039;D:\\\\DSCracker\\\\DS Cracker\\\\Python1&#039;\n\nprint(&quot;Before using shutil.copystat():&quot;)\nprint(&quot;Permission bits:&quot;,oct(os.stat(src).st_mode)&#x5B;-3:])\nprint(&quot;Last modification time:&quot;, time.ctime(os.stat(src).st_mtime)) \n\nprint(&quot;Modification time:&quot;,time.ctime(os.stat(src).st_mtime))\n\nshutil.copystat(src, dest) \n\nprint(&quot;After using shutil.copystat():&quot;)\nprint(&quot;Permission bits:&quot;,oct(os.stat(dest).st_mode)&#x5B;-3:])\nprint(&quot;Last modification time:&quot;, time.ctime(os.stat(dest).st_mtime)) \nprint(&quot;Modification time:&quot;,time.ctime(os.stat(dest).st_mtime))\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nBefore using shutil.copystat():\nPermission bits: 777\nLast modification time: Mon Dec  7 02:20:37 2020\nModification time: Mon Dec  7 02:20:37 2020\n\nAfter using shutil.copystat():\nPermission bits: 777\nLast modification time: Mon Dec  7 03:43:47 2020\nModification time: Mon Dec  7 03:43:47 2020\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Other functions in shutil module<\/h2>\n\n\n\n<p>Let&#8217;s go over the miscellaneous functions of the shutil module now. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. shutil.disk_usage(path)<\/h3>\n\n\n\n<p><em>shutil.disk_usage(path)<\/em> function returns the disk usage statistics about the given path names as tuple with attributes <em>total<\/em> which is total amount of memory, <em>used<\/em> which is used space and <em>free<\/em> which is free space in bytes.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\nimport os\n\npath = &#039;D:\\\\DSCracker\\\\DS Cracker\\\\NewPython\\\\python1&#039;\n\nstatistics=shutil.disk_usage(path)\n\nprint(statistics)\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nusage(total=1000203087872, used=9557639168, free=990645448704)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. shutil.which()<\/h3>\n\n\n\n<p><em>shutil.which()<\/em> function returns the path to an executable application which would run if the given command cmd was called. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport shutil\nimport os\n\ncmd=&#039;Python&#039;\n\nlocate = shutil.which(cmd) \n\nprint(locate)\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nC:\\Users\\AskPython\\AppData\\Local\\Microsoft\\WindowsApps\\Python.EXE\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we covered how we can copy, remove, and work with other operations on files and folders using the <strong>shutil module<\/strong> in python. Hope you all enjoyed it. Stay tuned!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/shutil.html\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">shutil-High-level file operations official docs<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey! In this tutorial, we will learn about the functions present in the shutil module of Python. So, let&#8217;s get started. Python&#8217;s shutil module provides us a number of high-level operations on files. We can copy and remove files and directories. Let&#8217;s get started with the module and learn the practical implementation of each of [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":11332,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-11261","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/11261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=11261"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/11261\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/11332"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=11261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=11261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=11261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}