Python Shutil Module: 10 Methods You Should Know

Firstly, the Python Shutil module in 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 like creating closing files once they are copied and focusing on the business logic.

How does the Python shutil module work?

The basic syntax to use shutil module is as follows:

import shutil
shutil.submodule_name(arguments)

File-Directory operations

1. Python shutil.copy()

shutil.copy(): 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’s permission mode, but another type of metadata of the file, like the file’s creation and modification, is not preserved.

import os 
  
# import the shutil module  
import shutil 
  
# write the path of the file
path = '/home/User'
  
# List all the files and directories in the given path
print("Before copying file:") 
print(os.listdir(path)) 
  
  
# write the Source path 
source = "/home/User/file.txt"
  
# Print the file permission of the source given
perms = os.stat(source).st_mode 
print("File Permission mode:", perms, "\n") 
  
# Write the Destination path 
destinationfile = "/home/User/file(copy).txt"
  
# Copy the content of source file to destination file 
dests = shutil.copy(source, destinationfile) 
  
# List files and directories of the path 
print("After copying file:") 
print(os.listdir(path)) 
  
# Print again all the file permission
perms = os.stat(destinationfile).st_mode 
print("File Permission mode:", perms) 
  
# Print path of of the file which is created
print("Destination path:", dests) 

Output:

Before copying file:
['hrithik.png', 'test.py', 'file.text', 'copy.cpp']
File permission mode: 33188

After copying file:
['hrithik.png', 'test.py',  'file.text', 'file(copy).txt', 'copy.cpp']
File permission mode: 33188 
Destination path: /home/User/file(copy).txt

Explanation:

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’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.

2. Python shutil.copy2()

Firstly, this function is just like the copy() function except for the fact that it maintains the metadata of the source file.

from shutil import *
import os
import time
import sys

def show_file_info(filename):
    stat_info = os.stat(filename)
    print '\tMode    :', stat_info.st_mode
    print '\tCreated :', time.ctime(stat_info.st_ctime)
    print '\tAccessed:', time.ctime(stat_info.st_atime)
    print '\tModified:', time.ctime(stat_info.st_mtime)

os.mkdir('example')
print ('SOURCE time: ')
show_file_info('shutil_copy2.py')
copy2('shutil_copy2.py', 'example')
print ('DESTINATION time:')
show_file_info('example/shutil_copy2.py')

Output:

SOURCE time:
        Mode    : 33188
        Created : Sat Jul 16 12:28:43 2020
        Accessed: Thu Feb 21 06:36:54 2021
        Modified: Sat Feb 19 19:18:23 2021
DESTINATION time:
        Mode    : 33188
        Created : Mon Mar 1 06:36:54 2021
        Accessed: Mon Mar 1 06:36:54 2021
        Modified: Tue Mar 2 19:18:23 2021 

Explanation:

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.

3. Python shutil.copyfile()

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.

import os
import shutil

print('BEFORE LIST:', os.listdir('.'))
shutil.copyfile('file_copy.py', 'file_copy.py.copy')
print('AFTER LIST:', os.listdir('.'))

Output:

Latracal:shutil Latracal$ python file_copy.py
BEFORE LIST: 
[' .DS_Store', 'file_copy.py']
AFTER LIST: 
[ .DS_Store', 'file_copy.py', 'file_copy.py.copy']

Explanation:

In this code, we have written the function copyfile() where the same file name gets copied for the new file. Just a copy is added in the new file name. See the output.

4. Python shutil.copytree()

This function copies the file and the subdirectories 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.

import pprint
import shutil
import os

shutil.copytree('../shutil', './Latracal')
pprint.pprint(os.listdir('./Latracal'))

Output:

Latracal:shutil Latracal$ clone—directory. py
[' .DS—Store' ,
'file_copy.py' ,
'file_copy_new.py'
'file_with_metadata.py' , 
'clone_directory. py']

Explanation:

In this code, we have written the function copytree() to get a duplicate of that file.

5. Python shutil.rmtree()

This function removes the particular file and subdirectory from the specified directory, which means the directory is deleted from the system.

import pprint
import shutil
import os

print('BEFORE:')
pprint.pprint(os.listdir('.'))

shutil.rmtree('Latracal')

print('\nAFTER:')
pprint.pprint(os.listdir('.'))

Output:

Latracal:shutil Latracal$ retove—dir.py
BEFORE:
['.DS_Store',
'file_copy.py',

'file_copy_new.py',
'remove_dir.py',

'copy_with_metadata.py',
'Latracal'
'clone_directory.py']


AFTER:
['.DS_Store',
'file—copy.py' ,
'file_copy_new.py',
'remove_dir.py',

'copy_with_metadata.py',
'clone_directory. py']

Explanation:

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.

6. shutil.which()

The which() a 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’s path.

import shutil
import sys

print(shutil.which('bsondump'))
print(shutil.which('no-such-program'))

output:

Latracal:shutil Latracal$  python find_file.py
/usr/10ca1/[email protected]/bin/bsondunp

Explanation:

In this code, we have written the function that () to find any of the files when required.

7. Python shutil.disk_usage()

This function is used to understand how much information is present in our file system by just calling the disk_usage() function.

import shutil

total_mem, used_mem, free_mem = shutil.disk_usage('.')
gb = 10 **9

print('Total: {:6.2f} GB'.format(total_mem/gb))
print('Used : {:6.2f} GB'.format(used_mem/gb))
print('Free : {:6.2f} GB'.format(free_mem/gb))

Output:

shubhm:shutil shubhmS py
Total:499.9ø GB
Used :187.72 GB
Free :3ø8.26 GB

Explanation:

In this code, we have written the function disk_usage() to learn about the total, used, and free disk space.

8. Python shutil.move()

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.

import shutil
shutil.move('hello.py','newdir/')

Output:

 'newdir/hello.py'

Explanation:

In this code, we have written the function move() to move the file or directory from one place to another.

9. Python shutil.make_archive()

This function builds an archive (zip or tar) of files in the root directory.

import shutil
import pprint

root_directory='newdir'
shutil.make_archive("newdirabcd","zip",root_directory)

output:

'C:\\python\\latracal\\newdirabcd.zip' 

Explanation:

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.

10. Python shutil.get_archive_formats()

This function gives us all the supported archive formats in the file or directory.

import shutil
import sys 

shutil.get_archive_formats()

output:

[('bztar', "bzip2'ed tar-file"), ('gztar', "gzip'ed tar-file"), ('tar', 'uncompressed tar file'), ('xztar', "xz'ed tar-file"), ('zip', 'ZIP file')]

Explanation:

In this code, we have written the function get_archive_formats() to get the supportive archive formats in the file or directory.

Advantages

  • The shutil module helps you automate the copying of files and directories.
  • This module saves the steps of opening, reading, writing, and closing files when there is no actual processing; it simply moves files.

Must Read

Conclusion

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 shutil module in Python.

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.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments