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
shutil.copyfileobj(fsrc,fdest[,length])- It helps copy data from source file object
fsrcto destination file object
lengthis 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
fsrc = open('c.txt', 'r') fdest = open('d.txt','a') shutil.copyfileobj(fsrc,fdest)
!cat d.txt | wc -l
shutil.copyfile(src,dest,follow_symlinks=True)- Copies conttents of file named
follow_symlinksis set to
srcis symlinks then it creates new symlinks rather than copying contents. If
destboth are same then it raises
shutil.copyfile('c.txt', 'e.txt') ## If destination file already exists then it overwrites and if it does not then creates one.
!cat e.txt | wc -l
destboth are symlinks and
Falsethen it tries to modify
destsymlink not the original file it's pointing to.
last access time,
last modified timeand
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
!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
srcto destination file or directory
destis directory then it'll copy
srcto that directory. If
Truethen it copies original file from
destelse it creates
shutil.copy2(src,dest,follow_symlinks=True)- Same as above method but tries to preserve metadata of
copystat()to copy metadata.
!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
namedtuple. It returns status in
path.user and group can be system user name or uid or gid as well.
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
cmdgets executed in shell/command prompt. Developer can also provide
pathto look for other wise it'll use
os.environ()path which will be
PATHvariable 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
If you are more comfortable learning through video tutorials then we would recommend that you subscribe to our YouTube channel.
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 firstname.lastname@example.org. 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.
If you want to