Updated On : Nov-24,2019 Time Investment : ~15 mins

Python "shutil" Module

shutil library helps the developer perform high-level operations like copying and removing on a bunch of files.

Note: High-level copying operations from a library does not copy metadata like ACLs, owner, group, etc.

import shutil
import glob
import os
data = """The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!"""
print('Creating directory structures and files for experimentation purpose.')
%mkdir folder_l1_1
%mkdir folder_l1_2
%mkdir folder_l1_1/folder_l2
!touch temp.txt temp.jpg temp.png a.png b.jpg c.txt d.txt t1.png t2.jpg .temp.png .temp.jpg
!touch folder_l1_1/t.txt folder_l1_1/t2.png folder_l1_1/a.mp4 folder_l1_1/b.mp4 folder_l1_1/c.mpeg folder_l1_1/t3.jpg
!touch folder_l1_2/t2.txt folder_l1_2/t1.png folder_l1_2/b.mp4 folder_l1_2/c.mp4 folder_l1_2/d.mpeg
!touch folder_l1_1/folder_l2/t.txt folder_l1_1/folder_l2/t2.png folder_l1_1/folder_l2/a.mp4
print('\nCurrent directory contents : ')
%ls
print('\nfolder_l1_1 directory contents : ')
%ls folder_l1_1
print('\nfolder_l1_2 directory contents :')
%ls folder_l1_2
print('\nfolder_l1_1/folder_l2 directory contents : ')
%ls folder_l1_1/folder_l2
Creating directory structures and files for experimentation purpose.

Current directory contents :
__notebook__.ipynb  a.png  c.txt  folder_l1_1/  t1.png  temp.jpg  temp.txt
__output__.json     b.jpg  d.txt  folder_l1_2/  t2.jpg  temp.png

folder_l1_1 directory contents :
a.mp4  b.mp4  c.mpeg  folder_l2/  t.txt  t2.png  t3.jpg

folder_l1_2 directory contents :
b.mp4  c.mp4  d.mpeg  t1.png  t2.txt

folder_l1_1/folder_l2 directory contents :
a.mp4  t.txt  t2.png
## Writing data string defined above to c.txt file.
with open('c.txt','w') as f:
    f.write(data)
!cat c.txt | wc -l ## Checking whether data got written to c.txt
20
  • shutil.copyfileobj(fsrc,fdest[,length]) - It helps copy data from source file object fsrc to destination file object fdest. If length is given then it copies data in chunks specified by length. If a negative value is given for length then it might try to copy whole data which might cause memory issues in case of big files. Also, copying starts from whichever position the file pointer is in the file. If it isn't at 0 positions then a copy will not start from the beginning. Default value of length is 16384.
fsrc = open('c.txt', 'r')
fdest = open('d.txt','a')
shutil.copyfileobj(fsrc,fdest)
!cat d.txt | wc -l
0
  • shutil.copyfile(src,dest,follow_symlinks=True) - Copies conttents of file named src to file dest. If follow_symlinks is set to False and src is symlinks then it creates new symlinks rather than copying contents. If src and dest both are same then it raises SameFileError.
shutil.copyfile('c.txt', 'e.txt') ## If destination file already exists then it overwrites and if it does not then creates one.
'e.txt'
!cat e.txt | wc -l
20
  • shutil.copystat(src,dest,follow_symlinks=True) - Copies permission bits from src to dest. If src and dest both are symlinks and follow_symlinks is False then it tries to modify dest symlink not the original file it's pointing to.
  • shutil.copymode(src,dest,follow_symlinks=True) - Copies permission bits,last access time, last modified time and flag from src to dst

Both above methods do not modify file contents, owner and group.

## Please make a note that a.png and b.jpg has different permission bits. 
## a.png and d.jpg has difference permission bits, creation time as well
!chown sys a.png
!touch d.png b.png d.jpg
!chmod 655 a.png
!ls -lrt a.png d.jpg b.jpg
-rw-r--r-- 1 root root 0 Feb 18 03:04 b.jpg
-rw-r-xr-x 1 sys  root 0 Feb 18 03:04 a.png
-rw-r--r-- 1 root root 0 Feb 18 03:04 d.jpg
shutil.copystat('a.png','b.jpg')
shutil.copymode('a.png','d.jpg')
!ls -lrt a.png d.jpg b.jpg ## Please make a note in permission change of d.jpg and b.jpg at begining.
-rw-r-xr-x 1 root root 0 Feb 18 03:04 b.jpg
-rw-r-xr-x 1 sys  root 0 Feb 18 03:04 a.png
-rw-r-xr-x 1 root root 0 Feb 18 03:04 d.jpg
  • shutil.copy(src,dest,follow_symlinks=True) - Copies src to destination file or directory dest. If dest is directory then it'll copy src to that directory. If follow_symlinks is True then it copies original file from src to dest else it creates symlink.
  • shutil.copy2(src,dest,follow_symlinks=True) - Same as above method but tries to preserve metadata of src file into dest.It uses copystat() to copy metadata.
shutil.copy('d.jpg','e.jpg'), shutil.copy2('d.jpg','f.jpg')
('e.jpg', 'f.jpg')
!ls -lrt d.jpg e.jpg f.jpg ## Please make a note that copy() did not copy metadata like creation/modifcation time. But copy2() did copy metadata.
-rw-r-xr-x 1 root root 0 Feb 18 03:04 f.jpg
-rw-r-xr-x 1 root root 0 Feb 18 03:04 d.jpg
-rw-r-xr-x 1 root root 0 Feb 18 03:04 e.jpg
  • shutil.disk_usage(path) - Returns disk usage stats of path as namedtuple. It returns status in bytes.
  • shutil.chown(path,user=None,group=None) - Change owner user and/or group of path.user and group can be system user name or uid or gid as well.
shutil.disk_usage(os.getcwd())
usage(total=5217320960, used=10788864, free=4938096640)
!ls -lrt d.jpg e.jpg
shutil.chown('d.jpg',user='sys')
shutil.chown('e.jpg',user='sys', group='sys')
!ls -lrt d.jpg e.jpg
-rw-r-xr-x 1 root root 0 Feb 18 03:04 d.jpg
-rw-r-xr-x 1 root root 0 Feb 18 03:04 e.jpg
-rw-r-xr-x 1 sys root 0 Feb 18 03:04 d.jpg
-rw-r-xr-x 1 sys sys  0 Feb 18 03:04 e.jpg
  • shutil.which(cmd,mode=os.F_OK|os.X_OK,path=None) - It returns path of executable which will run if cmd gets executed in shell/command prompt. Developer can also provide path to look for other wise it'll use os.environ() path which will be PATH variable of system.
print(shutil.which('rm'))
print(shutil.which('ls'))
print(shutil.which('pip'))
print(shutil.which('conda'))
print(shutil.which('wheel'))
print(shutil.which('chown'))
print(shutil.which('chmod'))
print(shutil.which('jupyter'))
print(shutil.which('ipython'))
/bin/rm
/bin/ls
/opt/conda/bin/pip
/opt/conda/bin/conda
/opt/conda/bin/wheel
/bin/chown
/bin/chmod
/opt/conda/bin/jupyter
/opt/conda/bin/ipython
Sunny Solanki  Sunny Solanki

YouTube Subscribe Comfortable Learning through Video Tutorials?

If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.

Need Help Stuck Somewhere? Need Help with Coding? Have Doubts About the Topic/Code?

When going through coding examples, it's quite common to have doubts and errors.

If you have doubts about some code examples or are stuck somewhere when trying our code, send us an email at coderzcolumn07@gmail.com. We'll help you or point you in the direction where you can find a solution to your problem.

You can even send us a mail if you are trying something new and need guidance regarding coding. We'll try to respond as soon as possible.

Share Views Want to Share Your Views? Have Any Suggestions?

If you want to

  • provide some suggestions on topic
  • share your views
  • include some details in tutorial
  • suggest some new topics on which we should create tutorials/blogs
Please feel free to contact us at coderzcolumn07@gmail.com. We appreciate and value your feedbacks. You can also support us with a small contribution by clicking DONATE.


Subscribe to Our YouTube Channel

YouTube SubScribe

Newsletter Subscription