Shutil Module in Python

Shutil Module In PYthon

Hey! In this tutorial, we will learn about the functions present in the shutil module of Python. So, let’s get started.

Python’s shutil module provides us a number of high-level operations on files. We can copy and remove files and directories. Let’s get started with the module and learn the practical implementation of each of the files in detail.

How to copy files with shutil module?

There are various methods available in shutil module to copy the contents of one file to another file.

1. shutil.copyfileobj(src,dst)

Suppose, we want to copy the contents of the file data.txt to data1.txt, we can use the follow piece of code:

import shutil

f=open('data.txt','r')
f1=open('data1.txt','w')

# Syntax: shutil.copyfileobj(src,dst)
shutil.copyfileobj(f,f1)

f.close()
f1.close()

2. shutil.copy(src,dst)

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.

import shutil
#shutil.copy(src.dst)
shutil.copy('data.txt','data1.txt')

3. shutil.copy2(src,dst)

copy(src,dst) and copy2(src,dst) functions are almost the same but copy2(src,dst) also copies the metadata of the source file.

Metadata includes the information about when the file was created, accessed or modified.

import shutil
#shutil.copy2(src,dst)
shutil.copy2('data.txt','data1.txt')

4. shutil.copyfile(src,dst)

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:

import shutil
import os

path='D:\DSCracker\DS Cracker\Python'
print("Before copying file:") 
print(os.listdir(path)) 

shutil.copyfile('data.txt','Python/data3.txt')

print("After copying file:") 
print(os.listdir(path))

Output:

Before copying file:
['hey.py']
After copying file:
['data3.txt', 'hey.py']

5. shutil.move(src,dst)

Suppose, we want to delete a file from one location and move it to another location. Here, let’s move shutil.py from source to a different location:

import shutil
import os

path='D:\DSCracker\DS Cracker'
print("Source folder:") 
print(os.listdir(path))

path1='D:\DSCracker\DS Cracker\Python'
shutil.move('shutil.py','Python')

print("After moving file shutil.py to destination folder, destination contains:") 
print(os.listdir(path1))

Output:

Source folder:
['cs', 'data.txt', 'Python', 'ReverseArray', 'ReverseArray.cpp', 'shutil.py']
After moving file shutill.py to destination folder, destination contains:
['data1.txt', 'data3.txt', 'hey.py', 'nsawk.py', 'shutil.py']

6. shutil.copytree(src,dst)

If we want to copy a complete folder which includes all its files to a new location, we can use copytree(src,dst) function.

It recursively copies an entire directory tree rooted at src to directory named dst and returns the destination directory.

Let’s copy the folder Python to the folder Newfolder.

Note: We have to create a new folder inside our destination folder as the function does not allow copying contents to an already existing folder.

So here, we have created the folder python1 inside the folder Newfolder.

import os
import shutil

path='D:\DSCracker\DS Cracker\Python'
print("Source folder:") 
print(os.listdir(path))

shutil.copytree('Python','NewPython/python1')

path1='D:\DSCracker\DS Cracker\NewPython\python1'
print("Destination folder:")
print(os.listdir(path1))

Output:

Source folder:
['data1.txt', 'data3.txt', 'hey.py', 'nsawk.py', 'shutill.py']
Destination folder:
['data1.txt', 'data3.txt', 'hey.py', 'nsawk.py', 'shutill.py']

How to remove/delete files with shutil module?

Now that we’ve learned how we can work with the moving and copying files, let’s learn to remove or delete files from specific locations from within your Python scripts.

By using shutil.rmtree(), we can delete any folder,file or directory. Let’s delete the folder Python.

import os
import shutil

path='D:\DSCracker\DS Cracker'
print("Before deleting:") 
print(os.listdir(path))

shutil.rmtree('Python')
print("After deleting:") 
print(os.listdir(path))

Output:

Before deleting:
['cs', 'data.txt', 'NewPython', 'program.py', 'Python', 'ReverseArray', 'ReverseArray.cpp']

After deleting:
['cs', 'data.txt', 'NewPython', 'program.py', 'ReverseArray', 'ReverseArray.cpp']

How to copy permission bits of one file to another?

Copying file is one part. What if you just want to copy the same permissions of a file to all other files? Let’s learn to do just that using the shutil module here.

1. shutil.copymode(src,dst)

This method copies the permission bits from src to dst. Let’s copy permission bits of Python directory to Python1 directory.

import shutil
import os
src= 'D:\\DSCracker\\DS Cracker\\Python'
dest='D:\\DSCracker\\DS Cracker\\Python1'

print("Before using shutil.copymode(), Permission bits of destination:")
print(oct(os.stat(dest).st_mode)[-3:])

shutil.copymode(src, dest) 
print("After using shutil.copymode(), Permission bit of destination:")
print(oct(os.stat(dest).st_mode)[-3:])

Output:

Before using shutil.copymode(), Permission bits of source:
677
After using shutil.copymode(), Permission bit of destination:
777

2. shutil.copystat(src,dst)

shutil.copystat(src.dst) copies permission bits along with metadata.

import shutil
import os
import time 

src= 'D:\\DSCracker\\DS Cracker\\Python'
dest='D:\\DSCracker\\DS Cracker\\Python1'

print("Before using shutil.copystat():")
print("Permission bits:",oct(os.stat(src).st_mode)[-3:])
print("Last modification time:", time.ctime(os.stat(src).st_mtime)) 

print("Modification time:",time.ctime(os.stat(src).st_mtime))

shutil.copystat(src, dest) 

print("After using shutil.copystat():")
print("Permission bits:",oct(os.stat(dest).st_mode)[-3:])
print("Last modification time:", time.ctime(os.stat(dest).st_mtime)) 
print("Modification time:",time.ctime(os.stat(dest).st_mtime))

Output:

Before using shutil.copystat():
Permission bits: 777
Last modification time: Mon Dec  7 02:20:37 2020
Modification time: Mon Dec  7 02:20:37 2020

After using shutil.copystat():
Permission bits: 777
Last modification time: Mon Dec  7 03:43:47 2020
Modification time: Mon Dec  7 03:43:47 2020

Other functions in shutil module

Let’s go over the miscellaneous functions of the shutil module now.

1. shutil.disk_usage(path)

shutil.disk_usage(path) function returns the disk usage statistics about the given path names as tuple with attributes total which is total amount of memory, used which is used space and free which is free space in bytes.

import shutil
import os

path = 'D:\\DSCracker\\DS Cracker\\NewPython\\python1'

statistics=shutil.disk_usage(path)

print(statistics)

Output:

usage(total=1000203087872, used=9557639168, free=990645448704)

2. shutil.which()

shutil.which() function returns the path to an executable application which would run if the given command cmd was called.

import shutil
import os

cmd='Python'

locate = shutil.which(cmd) 

print(locate)

Output:

C:\Users\AskPython\AppData\Local\Microsoft\WindowsApps\Python.EXE

Conclusion

In this tutorial, we covered how we can copy, remove, and work with other operations on files and folders using the shutil module in python. Hope you all enjoyed it. Stay tuned!

References

shutil-High-level file operations official docs