{"id":850,"date":"2022-09-07T14:47:00","date_gmt":"2022-09-07T09:17:00","guid":{"rendered":"https:\/\/geekpython.in\/?p=850"},"modified":"2023-08-15T16:27:54","modified_gmt":"2023-08-15T10:57:54","slug":"shutil-module-in-python","status":"publish","type":"post","link":"https:\/\/geekpython.in\/shutil-module-in-python","title":{"rendered":"Perform High-Level File Operations In Python &#8211; shutil Module"},"content":{"rendered":"\n<p>Copying or moving files or folders manually from one directory to another directory could be a real pain. This can be automated using a Python module called&nbsp;<code>shutil<\/code>.<\/p>\n\n\n\n<p>Shutil module provides some high-level operations on files and collection of files like copying, moving, or removing the files.<\/p>\n\n\n\n<p>In other words, the&nbsp;<code>shutil<\/code>&nbsp;module helps in automating the task of file copying or moving from one directory to another directory. It comes under the Python standard library so it doesn&#8217;t need to be installed externally.<\/p>\n\n\n\n<p>In this article, we&#8217;ll learn this module.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-copy-files-to-another-directory\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-copy-files-to-another-directory\"><\/a>Copy files to another directory<\/h1>\n\n\n\n<p><code>shutil.copy()<\/code>&nbsp;method is used to copy the content of the&nbsp;<strong><em>source<\/em><\/strong>&nbsp;file to the&nbsp;<strong>destination<\/strong>&nbsp;file or directory. While the process of copying also copies the file&#8217;s permission mode but other metadata like the file\u2019s creation and modification times, is not preserved.<\/p>\n\n\n\n<p>The source must represent a file but the destination can be a file or a directory. If the destination is a directory then the file will be copied to the destination directory with a base filename from the source. The destination must be writable.<\/p>\n\n\n\n<p>If the destination is a file and already exists then it will be replaced by the source file otherwise the new file will be created.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong>&nbsp;<code>shutil.copy(source, destination, *, follow_symlinks = True)<\/code><\/p>\n\n\n\n<p><strong>Parameter:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>source<\/code>: A string that shows the path of the source file.<\/li>\n\n\n\n<li><code>destination<\/code>: A string that shows the path of the destination file or directory.<\/li>\n\n\n\n<li><code>follow_symlinks<\/code>&nbsp;(optional): The default value of this parameter is&nbsp;<code>True<\/code>. If it is&nbsp;<code>False<\/code>&nbsp;and the source represents a symbolic link then the destination will be created as a symbolic link.<\/li>\n<\/ul>\n\n\n\n<p><strong>Copying source file to the destination<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\n\n# source and destination path\nsrc_path = \"hello.txt\"\ndst_path = \"hello2.txt\"\n\npath = shutil.copy(src_path, dst_path)\n\n# Printing the path of destination dir\nprint('Destination Path:', path)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Destination Path: hello2.txt<\/pre><\/div>\n\n\n\n<p><strong>If the destination is the directory<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import os\nimport shutil\n\n# Making a directory\nos.mkdir('Project')\n\n# Listing the files in it\nprint(\"Empty Folder: \", os.listdir('Project'))\n\n# source and destination path\nsrc_path = \"hello.txt\"\ndst_path = \"Project\"\n\n# Copying file to destination\npath = shutil.copy(src_path, dst_path)\n\n# Printing the name of copied filename and path of destination dir\nprint('File Copied:', os.listdir('Project'))\nprint('Destination Path:', path)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Empty Folder:  []\nFile Copied: ['hello.txt']\nDestination Path: Project\\hello.txt<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-copy-metadata-along-with-the-file\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-copy-metadata-along-with-the-file\"><\/a>Copy metadata along with the file<\/h1>\n\n\n\n<p><code>shutil.copy2()<\/code>&nbsp;method is used to copy the contents of the source file to the destination file or directory. It is the same as the&nbsp;<code>shutil.copy()<\/code>&nbsp;method but the only difference is that&nbsp;<strong>this method tries to preserve the metadata of the file<\/strong>.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong>&nbsp;<code>shutil.copy2(source, destination, *, follow_symlinks = True<\/code>)<\/p>\n\n\n\n<p><strong>Parameter:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>source<\/code>: A string that shows the path of the source file.<\/li>\n\n\n\n<li><code>destination<\/code>: A string that shows the path of the destination file or directory.<\/li>\n\n\n\n<li><code>follow_symlinks<\/code>&nbsp;(optional): The default value of this parameter is&nbsp;<code>True<\/code>. If it is&nbsp;<code>False<\/code>&nbsp;and the source represents a symbolic link then it attempts to copy all metadata from the source symbolic link to the newly-created destination symbolic link. This functionality is platform dependent.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import os\nimport shutil\n\n# Specifying src and dst paths\nsrc_path = \"Data\\geek.txt\"\ndst_path = \"NewData\"\n\nprint(\"Before copying the metadata:\")\n\n# Listing the files inside src folder\nsrc_folder = \"Data\"\nprint(os.listdir(src_folder))\n\n# Printing the status of the files inside src path\nprint(\"Metadata:\", os.stat(src_path), \"\\n\")\n\n# Copying content from src to dst\ndest = shutil.copy2(src_path, dst_path)\n\nprint(\"After copying the metadata:\")\n\n# Listing the files inside dst path\nprint(os.listdir(dst_path))\n\n# Printing the status of the files inside dst path\nprint(\"Metadata:\", os.stat(dest), \"\\n\")\n\n# Printing the destination path of newly created files\nprint(\"Files copied to:\", dest)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Before copying the metadata:\n['geek.txt', 'hello.py', 'python.txt']\nMetadata: os.stat_result(st_mode=33206, st_ino=7036874417909405, st_dev=3836766283, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1662197106, st_mtime=1662196237, st_ctime=1662194099) \n\nAfter copying the metadata:\n['geek.txt']\nMetadata: os.stat_result(st_mode=33206, st_ino=20547673300020899, st_dev=3836766283, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1662197106, st_mtime=1662196237, st_ctime=1662204751) \n\nFiles copied to: NewData\\geek.txt<\/pre><\/div>\n\n\n\n<p><strong>If the destination is a directory<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import os\nimport shutil\n\npath = \"NewData\"\n\nprint(\"Before copying the files:\")\n\n# Listing the files inside dst folder\nprint(os.listdir(path), \"\\n\")\n\n# Specifying src and dst paths\nsrc_path = \"Sample\/hello.txt\"\ndst_path = \"NewData\"\n\n# Copying content from src to dst\ndest = shutil.copy2(src_path, dst_path)\n\nprint(\"After copying the metadata:\")\n\n# Listing the files inside dst folder\nprint(os.listdir(path))\n\n# Printing the destination path of newly created files\nprint(\"Files copied to:\", dest)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Before copying the files:\n[] \n\nAfter copying the files:\n['hello.txt']\nFiles copied to: NewData\\hello.txt<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-copy-content-from-one-file-to-another-file\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-copy-content-from-one-file-to-another-file\"><\/a>Copy content from one file to another file<\/h1>\n\n\n\n<p><code>shutil.copyfile()<\/code>&nbsp;method is used to copy the content of the source file to the destination file without the metadata. The&nbsp;<strong>source<\/strong>&nbsp;and&nbsp;<strong>destination must represent a file<\/strong>&nbsp;and the destination file must be writable. If the destination already exists then it&#8217;ll be replaced or a new file will be created.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong>&nbsp;<code>shutil.copyfile(source, destination, *, follow_symlinks = True)<\/code><\/p>\n\n\n\n<p><strong>Parameter:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>source<\/code>: A string that shows the path of the source file.<\/li>\n\n\n\n<li><code>destination<\/code>: A string that shows the path of the destination file or directory.<\/li>\n\n\n\n<li><code>follow_symlinks<\/code>&nbsp;(optional): The default value of this parameter is&nbsp;<code>True<\/code>. If&nbsp;<code>False<\/code>&nbsp;and source represent a symbolic link then a new symbolic link will be created instead of copying the file.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\n\n# Specifying the src and dest file paths\nsrc_path = \"Data\/python.txt\"\ndst_path = \"NewData\/geek.txt\"\n\n# Copying the src file to dst file\ndest = shutil.copyfile(src_path, dst_path)\n\n# Printing the path of newly created file\nprint(\"File Copied to:\", dest)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">File Copied to: NewData\/geek.txt<\/pre><\/div>\n\n\n\n<p><strong>If the source and destination file path is the same<\/strong><\/p>\n\n\n\n<p>If it happens then a&nbsp;<code>SameFIleError<\/code>&nbsp;will be raised and we can handle the error by using&nbsp;<code>shutil.SameFileError<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\n\n# Specifying the dst file same as src file\nsrc_path = \"Data\/python.txt\"\ndst_path = \"Data\/python.txt\"\n\ntry:\n    dest = shutil.copyfile(src_path, dst_path)\n    print(\"File Copied to:\", dest)\n\n# Printing error \nexcept shutil.SameFileError:\n    print(\"Source and Destination represent the same file.\")<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Source and Destination represent the same file.<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-copy-the-directory-tree\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-copy-the-directory-tree\"><\/a>Copy the directory tree<\/h1>\n\n\n\n<p><code>shutil.copytree()<\/code>&nbsp;method is used to replicate the complete directory tree. It recursively copies the entire directory tree rooted at the&nbsp;<strong><em>source<\/em><\/strong>&nbsp;directory to the&nbsp;<strong><em>destination<\/em><\/strong>&nbsp;directory. The destination directory must not already exist. The new directory will be created during the process.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong>&nbsp;<code>shutil.copytree(src, dst, symlinks = False, ignore = None, copy_function = copy2, ignore_dangling_symlinks = False)<\/code><\/p>\n\n\n\n<p><strong>Parameter:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>src<\/code>: A string that shows the path of the source directory.<\/li>\n\n\n\n<li><code>dest<\/code>: A string that shows the path of the destination.<\/li>\n\n\n\n<li><code>symlinks<\/code>&nbsp;(optional): This parameter accepts&nbsp;<code>True<\/code>&nbsp;or&nbsp;<code>False<\/code>, depending on which the metadata of the original links or linked links will be copied to the new tree.<\/li>\n\n\n\n<li><code>ignore<\/code>&nbsp;(optional): If ignore is given, it must be a callable that will receive as its arguments the directory being visited by&nbsp;<code>copytree()<\/code>, and a list of its contents, as returned by&nbsp;<code>os.listdir()<\/code>.<\/li>\n\n\n\n<li><code>copy_function<\/code>&nbsp;(optional): The default value of this parameter is&nbsp;<code>copy2<\/code>, other copy function like&nbsp;<code>copy()<\/code>&nbsp;can be used for this parameter.<\/li>\n\n\n\n<li><code>ignore_dangling_symlinks<\/code>&nbsp;(optional): This parameter value when set to&nbsp;<code>True<\/code>&nbsp;is used to put a silence on the exception raised if the file pointed by the symlink doesn\u2019t exist.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\n\n# Specifying src directory\nsrc_path = \"Data\"\n# src dir tree will be copied to the destination\ndst_path = \"Project\"\n\n# Copy the entire src directory tree to the destination\ndest = shutil.copytree(src_path, dst_path)\nprint(\"File Copied to:\", dest)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">File Copied to: Project<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-remove-a-directory-tree\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-remove-a-directory-tree\"><\/a>Remove a directory tree<\/h1>\n\n\n\n<p><code>shutil.rmtree()<\/code>&nbsp;method is used to&nbsp;<strong>delete the entire directory tree<\/strong>, the specified path must point to a directory (but not to a symbolic link to a directory).<\/p>\n\n\n\n<p><strong>Syntax:<\/strong>&nbsp;<code>shutil.rmtree(path, ignore_errors=False, onerror=None)<\/code><\/p>\n\n\n\n<p><strong>Parameter:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>path<\/code>: A path-like object representing a file path. A path-like object is either a string or bytes object representing a path.<\/li>\n\n\n\n<li><code>ignore_errors<\/code>: If&nbsp;<code>ignore_errors<\/code>&nbsp;is true, errors resulting from failed removals will be ignored.<\/li>\n\n\n\n<li><code>oneerror<\/code>: If&nbsp;<code>ignore_errors<\/code>&nbsp;is false or omitted, such errors are handled by calling a handler specified by&nbsp;<code>onerror<\/code>.<\/li>\n<\/ul>\n\n\n\n<p><strong>Removing a top-level directory tree<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\n\n# Directory tree to be removed\ndir_to_be_removed = \"Sample\"\n\n# Removing a top-level dir\nremove = shutil.rmtree(dir_to_be_removed)\n\nprint(\"Successfully removed.\")<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Successfully removed.<\/pre><\/div>\n\n\n\n<p><strong>Removing a directory tree under a directory<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\nimport os\n\n# Directory location\npath = \"Sample\/NewDirectory\"\n\n# directory to be removed\ndir_to_be_removed = \"Python\"\n\n# joined the path\njoin_path = os.path.join(path, dir_to_be_removed)\n\n# removing directory\nshutil.rmtree(join_path)\nprint(\"Successfully removed.\")<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Successfully removed.<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-move-files-to-another-location\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-move-files-to-another-location\"><\/a>Move files to another location<\/h1>\n\n\n\n<p><code>shutil.move()<\/code>&nbsp;method is used to move the source file or a directory to the destination location. If the destination directory already exists then the source directory will move inside that directory.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong>&nbsp;<code>shutil.move(src, dst, copy_function=copy2)<\/code><\/p>\n\n\n\n<p><strong>Parameter:<\/strong><\/p>\n\n\n\n<p><code>src<\/code>: A string that shows the path of the source file.<\/p>\n\n\n\n<p><code>dst<\/code>: A string that shows the path of the destination file or directory.<\/p>\n\n\n\n<p><code>copy_function<\/code>(optional): The default value of this parameter is&nbsp;<code>copy2<\/code>. We can use other copy functions also.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\n\n# Specifying src and dst directories\nsrc_path = \"Project\"\ndst_path = \"NewProject\"\n\n# Moving src dir to dst dir\ndest = shutil.move(src_path, dst_path)\n\n# Printing the destination location\nprint(\"Directory moved to:\", dest)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Directory moved to: NewProject\\Project<\/pre><\/div>\n\n\n\n<p><strong>If the destination doesn&#8217;t exist<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\n\n# Specifying src directory\nsrc_path = \"New_dir\"\n# Non-existing directory\ndst_path = \"Moved_dir\"\n\n# Moving src dir to dst dir\ndest = shutil.move(src_path, dst_path)\n\n# Printing the destination location\nprint(\"Directory moved to:\", dest)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Directory moved to: Moved_dir<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-finding-executable-files\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-finding-executable-files\"><\/a>Finding executable files<\/h1>\n\n\n\n<p><code>shutil.which()<\/code>&nbsp;method is used to find the path to an executable file specified to the&nbsp;<code>cmd<\/code>&nbsp;argument. If no&nbsp;<code>cmd<\/code>&nbsp;is specified then it will return&nbsp;<code>None<\/code>.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong>&nbsp;<code>shutil.which(cmd, mode = os.F_OK | os.X_OK, path = None)<\/code><\/p>\n\n\n\n<p><strong>Parameter:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>cmd<\/code>: File in string format.<\/li>\n\n\n\n<li><code>mode<\/code>: This parameter specifies the mode by which the method should execute.<\/li>\n\n\n\n<li><code>os.F_OK<\/code>&nbsp;tests the existence of the path<\/li>\n\n\n\n<li><code>os.X_OK<\/code>&nbsp;checks if the path can be executed<\/li>\n\n\n\n<li><code>path<\/code>: This parameter specifies the path to be used, if no path is specified then the results of&nbsp;<code>os.environ()<\/code>&nbsp;are used.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\n\n# Executable file to be located\nexec_file = \"pip\"\n\n# Finding the location\nlocation = shutil.which(exec_file)\n\n# Printing the location of the executable file\nprint(location)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">D:\\SACHIN\\Python310\\Scripts\\pip.EXE<\/pre><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-more-cool-methods\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-more-cool-methods\"><\/a>More cool methods<\/h1>\n\n\n\n<p>There are some other cool methods that the&nbsp;<code>shutil<\/code>&nbsp;module provides other than the above-mentioned methods.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-copy-the-stats-of-a-file-or-a-directory\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-copy-the-stats-of-a-file-or-a-directory\"><\/a>Copy the stats of a file or a directory using shutil<\/h3>\n\n\n\n<p><code>shutil.copystat()<\/code>&nbsp;method is used to copy the stats of a file or a directory like permission bits, last access time, last modification time, and flags from the&nbsp;<strong>source<\/strong>&nbsp;to the&nbsp;<strong>destination<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\nimport os\nimport time\n\nsrc_path = \"D:\/SACHIN\/Pycharm\/Number-Converter\"\ndst_path = \"D:\/SACHIN\/Pycharm\/OpenCV\/project1.py\"\n\n# Printing metadata of source dir\nprint(\"Source Data\")\nprint(\"Modified at:\", time.ctime(os.stat(src_path).st_mtime))\nprint(\"Accessed at:\", time.ctime(os.stat(src_path).st_atime), \"\\n\")\n\n# Printing metadata of destination dir\nprint(\"Destination Data\")\nprint(\"Modified at:\", time.ctime(os.stat(dst_path).st_mtime))\nprint(\"Accessed at:\", time.ctime(os.stat(dst_path).st_atime), \"\\n\")\n\n# Copying stats of src to dst\nshutil.copystat(src_path, dst_path)\n\n# Printing metadata after using shutil.copystat() method\nprint(\"Destination data after using shutil.stat()\")\nprint(\"Modified at:\", time.ctime(os.stat(dst_path).st_mtime))\nprint(\"Accessed at:\", time.ctime(os.stat(dst_path).st_atime))<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Source Data\nModified at: Fri Jun 10 19:31:51 2022\nAccessed at: Mon Sep  5 16:22:19 2022 \n\nDestination Data\nModified at: Tue Jul 27 19:00:02 2021\nAccessed at: Mon Sep  5 16:23:27 2022 \n\nDestination data after using shutil.stat()\nModified at: Fri Jun 10 19:31:51 2022\nAccessed at: Mon Sep  5 16:22:19 2022<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-retrieve-disk-usage-stats\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-retrieve-disk-usage-stats\"><\/a>Retrieve disk usage stats<\/h3>\n\n\n\n<p><code>shutil.disk_usage(path)<\/code>&nbsp;method is used to get the&nbsp;<strong>statistics of disk usage<\/strong>&nbsp;of the specified&nbsp;<strong>path<\/strong>. The return value has attributes&nbsp;<strong>total<\/strong>,&nbsp;<strong>used<\/strong>, and&nbsp;<strong>free<\/strong>&nbsp;which are the amount(in bytes) of the total, used, and free space. A&nbsp;<strong><em>path<\/em><\/strong>&nbsp;may be a file or a directory.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\n\n# Checking the stats\ndisk_size = shutil.disk_usage(\"NewSample\")\n\n# Printing the stats\nprint(\"The stats are:\", disk_size)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">The stats are: usage(total=374277664768, used=33221120000, free=341056544768)<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-archive-a-directory\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-archive-a-directory\"><\/a>Archive a directory<\/h3>\n\n\n\n<p><code>shutil.make_archive()<\/code>&nbsp;is used to create an archive file in a&nbsp;<code>zip<\/code>&nbsp;or&nbsp;<code>tar<\/code>&nbsp;format.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\n\n# Path of directory to be archived\ndirectory = \"NewSample\"\n\n# Archiving the directory with zip format\narchive = shutil.make_archive(directory, format=\"zip\")\n\nprint(archive)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">NewSample.zip<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"heading-unpacking-a-directory\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-unpacking-a-directory\"><\/a>Unpacking a directory<\/h3>\n\n\n\n<p><code>shutil.unpack_archive()<\/code>&nbsp;is used to unpack the archive file. The method takes two arguments, the first is the&nbsp;<code>filename<\/code>&nbsp;which is the&nbsp;<strong>full path of the archive file<\/strong>&nbsp;and the second is the&nbsp;<code>directory<\/code>&nbsp;in which the files are extracted.<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">import shutil\n\n# Path of directory to be unpacked\ndirectory = \"NewSample.zip\"\n\n# Unpacking the archive dir\nshutil.unpack_archive(directory, \"extracted_dir\")\n\nprint(f\"Successfully extracted the files from {directory}.\")<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">Successfully extracted the files from NewSample.zip.<\/pre><\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>For performing the high-level archiving operations on files in Python, See&nbsp;<a target=\"_blank\" href=\"https:\/\/geekpython.in\/zipfile-read-and-write-zip-files-without-extracting-it-in-python\" rel=\"noreferrer noopener\">zipfile<\/a>&nbsp;module.<\/p>\n<\/blockquote>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"heading-conclusion\"><a href=\"https:\/\/geekpython.in\/shutil-module-in-python#heading-conclusion\"><\/a>Conclusion<\/h1>\n\n\n\n<p>We sure learned to handle high-level file operations using the&nbsp;<code>shutil<\/code>&nbsp;module. We&#8217;ve learned how to<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Copy the file or a directory to another directory<\/li>\n\n\n\n<li>Copy the metadata of the file<\/li>\n\n\n\n<li>Permanently move the file or a directory to another location<\/li>\n\n\n\n<li>Remove and copy the directory tree<\/li>\n<\/ul>\n\n\n\n<p>Along with these operations, we&#8217;ve seen some of the cool methods which are provided by the&nbsp;<code>shutil<\/code>&nbsp;module.<\/p>\n\n\n\n<p><strong>Reference:<\/strong>&nbsp;<a href=\"https:\/\/docs.python.org\/3.10\/library\/shutil.html\" target=\"_blank\" rel=\"noreferrer noopener\">docs.python.org\/3.10\/library\/shutil.html<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>That&#8217;s all for now<\/strong><\/p>\n\n\n\n<p><strong>Keep Coding\u270c\u270c<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Copying or moving files or folders manually from one directory to another directory could be a real pain. This can be automated using a Python module called&nbsp;shutil. Shutil module provides some high-level operations on files and collection of files like copying, moving, or removing the files. In other words, the&nbsp;shutil&nbsp;module helps in automating the task [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":852,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[2,42],"tags":[44,43,12,31],"class_list":["post-850","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-modules","tag-files","tag-modules","tag-python","tag-python3","entry","has-media"],"_links":{"self":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/comments?post=850"}],"version-history":[{"count":3,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/850\/revisions"}],"predecessor-version":[{"id":1361,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/posts\/850\/revisions\/1361"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media\/852"}],"wp:attachment":[{"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/media?parent=850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/categories?post=850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekpython.in\/wp-json\/wp\/v2\/tags?post=850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}