High Level File Operations Using shutil Module

Hello Guys welcome back, in this section of tutorial we will be dealing with some High-Level File operations. For this operation we will be using the shutil module. so lets see what all this module has to offer us.

shutil : High – level file operations

What is Shutil module in python?

This module provides us the flexibility of performing high-level operation on files or collections of files. It provides functions which help support file copying and removal.

Shutil modules supports many functions which can be handy in many ways, but for now lets stick to some of the functions which are easy enough to understand the working and have the basic knowledge of the module rather than getting confused which with lots of stuffs.

shutil.move

os module also helps us to perform file operations, if you have worked with it then you must have noticed that os module contains a function for renaming or moving files i.e., os.rename. but it is preferred to use shutil.move instead, because it is more robust than os.rename.

This function move a file or directory (src) to another location (dest) [ Note : Recursively ]

If the destination is a directory, then src is moved inside that directory.

Understand the things better with an Example :

>>> import shutil
>>> shutil.move("/Users/ankurgupta/Desktop/xlsx as attachment.png","/Users/ankurgupta/Desktop/Dest Folder")
>>> 
Here is a simple code that moves the file to the destination location.

shutil.copy

This function copies the source file to the destination directory, but note that destination directory has to exist. If the filename is already present then in that case it will be overwritten.

The following properties are also update – access time and last modification time, and the permission of the files are also copied along with the content.

Example :

>>> import shutil                                                               >>> shutil.copy("/Users/ankurgupta/Desktop/ATM.py","/Users/ankurgupta/Desktop/Dest Folder”)
>>> import os
>>> os.listdir("/Users/ankurgupta/Desktop/Dest Folder")
['.DS_Store', 'ATM.py', 'xlsx as attachment.png']

shutil.copytree

This function recursively copies the directory tree rooted at source to destination. If any Error while performing the operation will be reported to the Standard Output. The Destination directory need not exist.

Example :

>>> import os
>>> os.listdir("/Users/ankurgupta/Desktop/-[NIC-VATSoft]-_files")
['.DS_Store', 'emb.bmp', 'StyleSheet.css']
>>> os.listdir("/Users/ankurgupta/Desktop/Dest Folder")
['.DS_Store', 'ATM.py', 'xlsx as attachment.png']
>>> import shutil
>>> shutil.copytree("/Users/ankurgupta/Desktop/-[NIC-VATSoft]-_files","/Users/ankurgupta/Desktop/Dest Folder1")
>>> os.listdir("/Users/ankurgupta/Desktop/Dest Folder1")                        ['.DS_Store', 'emb.bmp', 'StyleSheet.css']
>>>

shutil.rmtree

This function recursively deletes a directory tree

Example :

>>> import shutil
>>> shutil.rmtree("/Users/ankurgupta/Desktop/Dest Folder1") 
>>> os.listdir("/Users/ankurgupta/Desktop/Dest Folder1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: '/Users/ankurgupta/Desktop/Dest Folder1'
>>>

Here we get the Error because the mentioned directory does not exists as it is deleted.

 

shutil.copyfile

This function copy data from src to dest, in this case both names must be files. In short this function is used to copy files by name.

Here is syntax for the same :

import shutil 
shutil.copyfile('/path/to/file', ‘/path/to/other/phile')

Folks this is not the end of the shuttle module it is really very vast and will be definitely useful if you working with any High-Level File Operations.

>>> dir(shutil)
['Error', 'ExecError', 'SpecialFileError', 'WindowsError', '_ARCHIVE_FORMATS', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_basename', '_call_external_zip', '_destinsrc', '_get_gid', '_get_uid', '_make_tarball', '_make_zipfile', '_samefile', 'abspath', 'collections', 'copy', 'copy2', 'copyfile', 'copyfileobj', 'copymode', 'copystat', 'copytree', 'errno', 'fnmatch', 'get_archive_formats', 'getgrnam', 'getpwnam', 'ignore_patterns', 'make_archive', 'move', 'os', 'register_archive_format', 'rmtree', 'stat', 'sys', 'unregister_archive_format']
>>>

Here are the list of functions that are supported by shutil module.

You can learn more about it by visiting the Python Official Documentation

https://docs.python.org/2/library/shutil.html

So in case of any queries or question do reach us, We will help you in best possible way to keep the things going in you favour, Thank you.