[no audio]
Friends here our concept is Python Shutil Module. See this
Shutil Module is the default module with your Python 2 or Python 3, and
this module provides us a number of high-level operations
on files and directories.
They are like copy, move, remove and much more. See using OS
module also we can do some file operations, but compared
to OS module, Shutil Module is having advanced level operations.
First let me show you the list of operations or functions
which are available with your Shutil Module.
So, we have some syntax to list them.
Let me open my terminal Python Shell. First let me
'import shutil' module, then simply use 'dir(shutil)'.
If you observe here, we are having different types of error,
exception handling, and some operations with your
Shutil Module. There are different types of operations, right. So
here I am going to explain about your Shutil Module step-by-step.
So, in this part we are going to discuss about operations
with 'copy' related thing. Suppose if you want to copy files
or directories using Shutil Module, then what are the functions
available with your Shutil Module, right?
See that? If you observe here - 'copy', 'copy2', 'copyfile',
these are the copy related operations with your Shutil Module.
And one more thing, you can use the Shutil Module with Unix-like
system also. So this is my CentOS. Let me enter into my
Python 3 and here also you can import your 'shutil', and you
can see the list of operations, right?
So here, our concept is how to copy files with your
Shutil Module. So, to copy we are having different types of functions
with your Shutil Module. Why these many operations or functions?
Let me explain them step-by-step.
So, suppose we are going to discuss all these things, right?
So, first of all, let me take my complete 'python' path.
[no audio]
So, this is my 'python3' path on this host.
[no audio]
So, I've already taken a script name as 'shutil_part_1.py'
now. So if you want to copy a file you need source and destination.
First let me take 'src',
I mean some file. So I am taking from this location one of the file.
Let me take one-by-one.
So 'src', in this location, I have suppose this script, so I am taking that.
Then destination also I am taking same path first.
[no audio]
So, if you take same path there is no use. But in case, if we
take same path, what will happen?
'shutil.copyfile'.
So guys, if you go in this order, then it is very easy to
understand about all these operations.
So, I will write my order to explain about 'copy' operations
with your Shutil Module. That's fine.
So, 'shutil.copyfile(src,dest)'.
[no audio]
Now, just come here.
So, you have this script. So now let me run this and see the result.
[no audio]
What happened?
Yeah, I forgot to import Shutil Module.
Let me import that first.
[no audio]
Now run that and see the result.
'SameFileError'.
So, your Shutil Module is giving an exception: 'SameFileError'.
Actually, if you want to copy a file from source to destination
by taking source and destination as same, with same file name,
then there is no use. Suppose
I want to take a backup, then there is some use, right?
So, I am taking this destination as some backup file. Now if you run this
[no audio]
So I'm running our script. Now it's done.
Now again, see list files, 'working_with_remote_server.py',
this is the file, and we have taken backup for that,
right? And if you observe here, this file was created, suppose
September 22nd, but this file created just now.
And what are the permissions here?
These permissions and these permissions are completely different,
right? But while taking backup, suppose I want to have same
permissions of my source file, then don't use 'copyfile', then use 'copy' simply,
'shutil.copy(src,dest)'. So before going to take it, I am removing
previous backup file.
[no audio]
Let's be clear.
So why I am taking 'copy' instead of 'copyfile'?
See if we take 'copyfile',
it is not preserving the permissions of your source
file to your destination file.
But if you go with 'copy', whatever the permissions are there
with your source file, same permissions will be there for
your destination file as well.
Now, let me run our script and after that just see that, this
is the backup file just now we have taken using your 'copy'
operation and see the permissions here and here. Now they
are same. But apart from permissions, I want to have the entire
metadata as it is, whatever it is there for your source file,
that entire metadata I want for your destination file as
well. That means timestamp also I don't want to change.
I want to get this timestamp for my backup file.
Then use 'copy' to module.
[no audio]
So, guys if you come from here to here,
[no audio]
permissions.
[no audio]
So why I am taking 'copy' operations?
[no audio]
Same permissions for source and destination.
So now I am using 'shutil.copy2' operation.
[no audio]
So before that, let me remove once again my backup file and
now I am running our script. Now see the result.
For your
[no audio]
backup file and your original source and destination, now for
both the files you are having same permissions and same timestamp, right?
[no audio]
So same metadata for destination as well.
That's it.
So now we're done with 'copyfile' first, then 'copy', then 'copy2',
right? 'copy', then go with 'copy2'. Now you have some idea why we are
using 'copyfile' or 'copy' or 'copy2', right? Now, let me go with
'copymode' first.
We're done with these three, right? Now,
let me go with 'copymode'. See nothing is there, 'copymode'.
Whatever the permissions are there with your source file,
that permissions only, I want to get for my some destination file.
But here, make sure that it is not copying a file.
It is giving only permissions of source files to destination
files. See that,
[no audio]
suppose if you observe here, let me
[no audio]
Yeah, for this only I am giving some permissions, 700
[no audio]
So, for this Shutil Module we are having 700
permissions. So, this permissions I want to provide for this
file. Then, take this as the source file. Very simple guys.
[no audio]
Whatever the permissions are there with your source file,
that permissions will be given to your destination file. Now,
let me comment this.
So, before running our script,
[no audio]
yeah we need to add syntax as well, right.
[no audio]
'shutil.copymode(src,dest)'.
Be clear, it is giving only permissions of your source files to destination
file. Make sure that your source and destination must exist, right? Now,
[no audio]
so before running our Python script, what is the permissions of this?
775. 7-7-5. Now, we are providing these permissions to this file.
So, we have given for that logic in our script.
So now check it.
So, what are the permissions are there for your backup file?
Whatever the permissions are there for this script that
permissions are provided to your destination file, but we
are not copying, see that. 'cat'
[no audio]
Right, this is different content. This file is different
content. So, with our logic we are providing only
modes, permissions of this file to this file, but we are not copying, be clear.
That's fine. Then 'copystat'. See why we are using 'copy2' operation?
While copying I want to get entire metadata of this file to destination
file. Suppose you already created your destination file, but
you want to get some metadata of some file to, I mean, you
want to assign same metadata of a file to another file, nothing
is there. Suppose, just observe that. So we created this file November 1.
Now what other data is there here, I mean date is there?
22. So along with permissions,
I want to assign this timestamp to my file.
Then you have to use 'copystat'.
[no audio]
Simply 'shutil.copystat(src,dest)',
[no audio]
right? Now, let me run this and see the result.
[no audio]
Sorry, 'src'. Spelling mistake.
[no audio]
So, guys previously your backup file timestamp is this one. Now,
we are assigning this timestamp to, I mean, this timestamp
to your file. Now, just observe that.
See that, timestamp of your
backup file and your Python script are same, and permissions as well,
so entire metadata will be assigned to your destination file.
Make sure that you are not copying the content of your file to your destination file.
You are giving only permissions.
[no audio]
That's it. See the content of this file is different.
This is the content. And the content of this file is different, right?
[no audio]
This file is different. That's it.
[no audio]
That's fine. Now, yeah guys, this 'copyfileobj' is nothing but you
know you can open a file using 'open' or 'read' mode, and you
can write that content into some other file using 'write' mode.
So, the file object whatever you created that file object
can be copied from one file object to another file object using
'copyfileobj'. Just assume that, you just derive it, this
logic. Just suppose, assume that there is a file called 'xyz.txt',
I'm opening that in 'read' mode, and I'm creating one more file object,
[no audio]
some 'pqr.txt' but this is in 'write' mode.
[no audio]
Right? Now if you want to copy the content of f1 to f2 then use
'shutil.copyfileobj(f1,f2)'. That's it. You just try with this.
Now, we are pending with one more operation that is 'copytree'.
Nothing is there, 'copytree' is useful to clone or copy entire
directory recursively to your destination. Let's say, suppose
[no audio]
I'm having some tomcat9.
So, if you observe that, tomcat9, we are having
different directory structure for that,
I mean different files and folders in that, right? Now,
what I want to do is, I want to copy that entire directory
structure into some other file. Let's say
'copytree'. Let me take now source as
I'm going to create new source guys.
[no audio]
'tomcat7', suppose.
[no audio]
Now, 'src', then 'dest' directly I am giving
under 'tmp', suppose
[no audio]
'tomcat7'. You can take any name, 'tomcat78',
'tomcat', suppose some '7.8', something like that.
Right now, let me run this.
So before going to run, let me check it, is there any tomcat9?
I mean something 'tomcat7', or simply 'tomcat'. That is your wish.
You can take any destination directory.
We don't have any tomcat here. Now
let me run our Python script.
Sorry. 'src', I missed the '/' here.
It's done.
[no audio]
tomcat has been created.
[no audio]
And make sure that it is giving entire metadata, whatever
you are having for your source that metadata will be provided
for your destination as well, using 'copytree' operation from
your Shutil Module. That's it.
Right. Your last point, suppose guys if you want
to remove, if you want to remove a directory structure
recursively, then simply you can use
[no audio]
'shutil.rmtree', simply your destination.
So, I am going to remove this path only. Directly
you can provide your destination or you can store your
destination into some variable, some 'dest' variable, and then
you can use that variable, otherwise directly
you can provide your source and destination paths in your
operation itself, so 'rmtree'. So it will remove the entire
directory structure recursively.
So, suppose if I run that.
And see the 'tmp' location, you don't have tomcat here. That's it. So guys,
these are the simple copy file operations with your Shutil Module, okay. Very useful
your devop site, or some Linux admin site, or maybe
any admin, like WebLogic or database because you are going
to take some backups, right, and in your database site,
sometimes you need to create same timestamp for your destination as well.
So, at that time you can use simply 'copy2' operation from your Shutil Module,
okay. Okay guys, thank you for watching this video.