1 00:00:00,000 --> 00:00:01,800 [no audio] 2 00:00:01,800 --> 00:00:05,600 Friends here our concept is Python Shutil Module. See this 3 00:00:05,600 --> 00:00:10,600 Shutil Module is the default module with your Python 2 or Python 3, and 4 00:00:10,600 --> 00:00:14,400 this module provides us a number of high-level operations 5 00:00:14,400 --> 00:00:16,309 on files and directories. 6 00:00:16,300 --> 00:00:21,200 They are like copy, move, remove and much more. See using OS 7 00:00:21,200 --> 00:00:25,000 module also we can do some file operations, but compared 8 00:00:25,000 --> 00:00:30,210 to OS module, Shutil Module is having advanced level operations. 9 00:00:30,200 --> 00:00:33,400 First let me show you the list of operations or functions 10 00:00:33,400 --> 00:00:35,900 which are available with your Shutil Module. 11 00:00:35,900 --> 00:00:38,200 So, we have some syntax to list them. 12 00:00:38,800 --> 00:00:43,300 Let me open my terminal Python Shell. First let me 13 00:00:43,300 --> 00:00:47,300 'import shutil' module, then simply use 'dir(shutil)'. 14 00:00:48,800 --> 00:00:52,100 If you observe here, we are having different types of error, 15 00:00:52,100 --> 00:00:55,500 exception handling, and some operations with your 16 00:00:55,500 --> 00:00:59,900 Shutil Module. There are different types of operations, right. So 17 00:00:59,900 --> 00:01:04,500 here I am going to explain about your Shutil Module step-by-step. 18 00:01:05,400 --> 00:01:10,310 So, in this part we are going to discuss about operations 19 00:01:10,300 --> 00:01:14,700 with 'copy' related thing. Suppose if you want to copy files 20 00:01:14,700 --> 00:01:19,000 or directories using Shutil Module, then what are the functions 21 00:01:19,000 --> 00:01:21,800 available with your Shutil Module, right? 22 00:01:21,800 --> 00:01:26,900 See that? If you observe here - 'copy', 'copy2', 'copyfile', 23 00:01:26,900 --> 00:01:31,400 these are the copy related operations with your Shutil Module. 24 00:01:31,400 --> 00:01:34,800 And one more thing, you can use the Shutil Module with Unix-like 25 00:01:34,800 --> 00:01:39,900 system also. So this is my CentOS. Let me enter into my 26 00:01:39,900 --> 00:01:44,100 Python 3 and here also you can import your 'shutil', and you 27 00:01:44,100 --> 00:01:48,310 can see the list of operations, right? 28 00:01:48,300 --> 00:01:52,800 So here, our concept is how to copy files with your 29 00:01:52,800 --> 00:01:57,100 Shutil Module. So, to copy we are having different types of functions 30 00:01:57,100 --> 00:02:01,110 with your Shutil Module. Why these many operations or functions? 31 00:02:01,100 --> 00:02:03,200 Let me explain them step-by-step. 32 00:02:03,800 --> 00:02:06,200 So, suppose we are going to discuss all these things, right? 33 00:02:07,400 --> 00:02:11,600 So, first of all, let me take my complete 'python' path. 34 00:02:11,600 --> 00:02:15,910 [no audio] 35 00:02:15,900 --> 00:02:19,900 So, this is my 'python3' path on this host. 36 00:02:19,900 --> 00:02:23,810 [no audio] 37 00:02:23,800 --> 00:02:28,400 So, I've already taken a script name as 'shutil_part_1.py' 38 00:02:28,400 --> 00:02:34,100 now. So if you want to copy a file you need source and destination. 39 00:02:34,100 --> 00:02:36,900 First let me take 'src', 40 00:02:37,400 --> 00:02:43,410 I mean some file. So I am taking from this location one of the file. 41 00:02:43,400 --> 00:02:44,910 Let me take one-by-one. 42 00:02:44,900 --> 00:02:51,100 So 'src', in this location, I have suppose this script, so I am taking that. 43 00:02:52,000 --> 00:02:54,800 Then destination also I am taking same path first. 44 00:02:54,800 --> 00:02:58,410 [no audio] 45 00:02:58,400 --> 00:03:01,600 So, if you take same path there is no use. But in case, if we 46 00:03:01,600 --> 00:03:03,100 take same path, what will happen? 47 00:03:04,600 --> 00:03:07,300 'shutil.copyfile'. 48 00:03:08,100 --> 00:03:11,100 So guys, if you go in this order, then it is very easy to 49 00:03:11,100 --> 00:03:13,310 understand about all these operations. 50 00:03:13,300 --> 00:03:18,800 So, I will write my order to explain about 'copy' operations 51 00:03:18,800 --> 00:03:23,010 with your Shutil Module. That's fine. 52 00:03:23,000 --> 00:03:26,500 So, 'shutil.copyfile(src,dest)'. 53 00:03:26,500 --> 00:03:28,709 [no audio] 54 00:03:28,700 --> 00:03:30,000 Now, just come here. 55 00:03:31,000 --> 00:03:36,600 So, you have this script. So now let me run this and see the result. 56 00:03:36,600 --> 00:03:39,100 [no audio] 57 00:03:39,100 --> 00:03:40,000 What happened? 58 00:03:40,400 --> 00:03:43,810 Yeah, I forgot to import Shutil Module. 59 00:03:43,800 --> 00:03:45,100 Let me import that first. 60 00:03:45,200 --> 00:03:48,310 [no audio] 61 00:03:48,300 --> 00:03:50,100 Now run that and see the result. 62 00:03:51,100 --> 00:03:52,500 'SameFileError'. 63 00:03:53,700 --> 00:03:58,000 So, your Shutil Module is giving an exception: 'SameFileError'. 64 00:03:58,000 --> 00:04:02,800 Actually, if you want to copy a file from source to destination 65 00:04:02,800 --> 00:04:05,700 by taking source and destination as same, with same file name, 66 00:04:05,700 --> 00:04:06,900 then there is no use. Suppose 67 00:04:06,900 --> 00:04:09,910 I want to take a backup, then there is some use, right? 68 00:04:09,900 --> 00:04:14,209 So, I am taking this destination as some backup file. Now if you run this 69 00:04:14,200 --> 00:04:17,110 [no audio] 70 00:04:17,100 --> 00:04:20,810 So I'm running our script. Now it's done. 71 00:04:20,800 --> 00:04:25,700 Now again, see list files, 'working_with_remote_server.py', 72 00:04:25,700 --> 00:04:28,700 this is the file, and we have taken backup for that, 73 00:04:30,000 --> 00:04:34,100 right? And if you observe here, this file was created, suppose 74 00:04:34,100 --> 00:04:37,200 September 22nd, but this file created just now. 75 00:04:38,800 --> 00:04:40,200 And what are the permissions here? 76 00:04:41,200 --> 00:04:45,000 These permissions and these permissions are completely different, 77 00:04:46,000 --> 00:04:52,200 right? But while taking backup, suppose I want to have same 78 00:04:52,200 --> 00:04:59,500 permissions of my source file, then don't use 'copyfile', then use 'copy' simply, 79 00:05:00,500 --> 00:05:07,000 'shutil.copy(src,dest)'. So before going to take it, I am removing 80 00:05:08,100 --> 00:05:09,400 previous backup file. 81 00:05:09,400 --> 00:05:12,400 [no audio] 82 00:05:12,400 --> 00:05:13,400 Let's be clear. 83 00:05:14,400 --> 00:05:18,800 So why I am taking 'copy' instead of 'copyfile'? 84 00:05:19,300 --> 00:05:22,100 See if we take 'copyfile', 85 00:05:22,400 --> 00:05:27,200 it is not preserving the permissions of your source 86 00:05:27,200 --> 00:05:29,300 file to your destination file. 87 00:05:30,500 --> 00:05:33,300 But if you go with 'copy', whatever the permissions are there 88 00:05:33,300 --> 00:05:36,400 with your source file, same permissions will be there for 89 00:05:36,400 --> 00:05:37,800 your destination file as well. 90 00:05:38,700 --> 00:05:45,200 Now, let me run our script and after that just see that, this 91 00:05:45,200 --> 00:05:50,010 is the backup file just now we have taken using your 'copy' 92 00:05:50,000 --> 00:05:55,200 operation and see the permissions here and here. Now they 93 00:05:55,200 --> 00:06:02,900 are same. But apart from permissions, I want to have the entire 94 00:06:02,900 --> 00:06:07,810 metadata as it is, whatever it is there for your source file, 95 00:06:07,800 --> 00:06:11,200 that entire metadata I want for your destination file as 96 00:06:11,200 --> 00:06:15,010 well. That means timestamp also I don't want to change. 97 00:06:15,000 --> 00:06:17,900 I want to get this timestamp for my backup file. 98 00:06:18,900 --> 00:06:21,700 Then use 'copy' to module. 99 00:06:21,700 --> 00:06:24,510 [no audio] 100 00:06:24,500 --> 00:06:26,800 So, guys if you come from here to here, 101 00:06:26,800 --> 00:06:30,310 [no audio] 102 00:06:30,300 --> 00:06:31,100 permissions. 103 00:06:31,100 --> 00:06:33,300 [no audio] 104 00:06:33,300 --> 00:06:36,000 So why I am taking 'copy' operations? 105 00:06:36,000 --> 00:06:38,810 [no audio] 106 00:06:38,800 --> 00:06:44,000 Same permissions for source and destination. 107 00:06:44,700 --> 00:06:49,100 So now I am using 'shutil.copy2' operation. 108 00:06:49,100 --> 00:06:52,409 [no audio] 109 00:06:52,400 --> 00:06:56,300 So before that, let me remove once again my backup file and 110 00:06:56,300 --> 00:07:00,900 now I am running our script. Now see the result. 111 00:07:02,200 --> 00:07:03,000 For your 112 00:07:03,000 --> 00:07:05,409 [no audio] 113 00:07:05,400 --> 00:07:09,200 backup file and your original source and destination, now for 114 00:07:09,200 --> 00:07:15,000 both the files you are having same permissions and same timestamp, right? 115 00:07:15,000 --> 00:07:17,300 [no audio] 116 00:07:17,300 --> 00:07:23,300 So same metadata for destination as well. 117 00:07:24,300 --> 00:07:25,300 That's it. 118 00:07:26,100 --> 00:07:31,810 So now we're done with 'copyfile' first, then 'copy', then 'copy2', 119 00:07:31,800 --> 00:07:39,409 right? 'copy', then go with 'copy2'. Now you have some idea why we are 120 00:07:39,400 --> 00:07:44,200 using 'copyfile' or 'copy' or 'copy2', right? Now, let me go with 121 00:07:44,200 --> 00:07:45,300 'copymode' first. 122 00:07:46,500 --> 00:07:48,100 We're done with these three, right? Now, 123 00:07:48,100 --> 00:07:52,710 let me go with 'copymode'. See nothing is there, 'copymode'. 124 00:07:52,700 --> 00:07:56,100 Whatever the permissions are there with your source file, 125 00:07:56,100 --> 00:08:00,300 that permissions only, I want to get for my some destination file. 126 00:08:00,300 --> 00:08:03,400 But here, make sure that it is not copying a file. 127 00:08:03,400 --> 00:08:06,600 It is giving only permissions of source files to destination 128 00:08:06,600 --> 00:08:08,200 files. See that, 129 00:08:08,200 --> 00:08:10,400 [no audio] 130 00:08:10,400 --> 00:08:12,600 suppose if you observe here, let me 131 00:08:12,600 --> 00:08:14,400 [no audio] 132 00:08:14,400 --> 00:08:18,700 Yeah, for this only I am giving some permissions, 700 133 00:08:18,700 --> 00:08:21,110 [no audio] 134 00:08:21,100 --> 00:08:25,210 So, for this Shutil Module we are having 700 135 00:08:25,200 --> 00:08:30,500 permissions. So, this permissions I want to provide for this 136 00:08:30,500 --> 00:08:35,900 file. Then, take this as the source file. Very simple guys. 137 00:08:35,900 --> 00:08:42,409 [no audio] 138 00:08:42,400 --> 00:08:47,210 Whatever the permissions are there with your source file, 139 00:08:47,200 --> 00:08:54,299 that permissions will be given to your destination file. Now, 140 00:08:54,299 --> 00:08:55,100 let me comment this. 141 00:08:56,500 --> 00:08:58,000 So, before running our script, 142 00:08:58,000 --> 00:09:00,100 [no audio] 143 00:09:00,100 --> 00:09:03,000 yeah we need to add syntax as well, right. 144 00:09:03,000 --> 00:09:05,000 [no audio] 145 00:09:05,000 --> 00:09:10,700 'shutil.copymode(src,dest)'. 146 00:09:11,400 --> 00:09:15,700 Be clear, it is giving only permissions of your source files to destination 147 00:09:15,700 --> 00:09:22,100 file. Make sure that your source and destination must exist, right? Now, 148 00:09:22,100 --> 00:09:24,100 [no audio] 149 00:09:24,100 --> 00:09:28,600 so before running our Python script, what is the permissions of this? 150 00:09:30,400 --> 00:09:37,110 775. 7-7-5. Now, we are providing these permissions to this file. 151 00:09:37,100 --> 00:09:40,410 So, we have given for that logic in our script. 152 00:09:40,400 --> 00:09:41,700 So now check it. 153 00:09:42,800 --> 00:09:45,410 So, what are the permissions are there for your backup file? 154 00:09:45,400 --> 00:09:49,200 Whatever the permissions are there for this script that 155 00:09:49,200 --> 00:09:53,100 permissions are provided to your destination file, but we 156 00:09:53,100 --> 00:09:56,200 are not copying, see that. 'cat' 157 00:09:56,200 --> 00:10:00,000 [no audio] 158 00:10:00,000 --> 00:10:04,100 Right, this is different content. This file is different 159 00:10:04,100 --> 00:10:08,800 content. So, with our logic we are providing only 160 00:10:08,800 --> 00:10:13,800 modes, permissions of this file to this file, but we are not copying, be clear. 161 00:10:15,100 --> 00:10:22,200 That's fine. Then 'copystat'. See why we are using 'copy2' operation? 162 00:10:23,000 --> 00:10:29,000 While copying I want to get entire metadata of this file to destination 163 00:10:29,000 --> 00:10:33,000 file. Suppose you already created your destination file, but 164 00:10:33,000 --> 00:10:36,900 you want to get some metadata of some file to, I mean, you 165 00:10:36,900 --> 00:10:41,710 want to assign same metadata of a file to another file, nothing 166 00:10:41,700 --> 00:10:48,510 is there. Suppose, just observe that. So we created this file November 1. 167 00:10:48,500 --> 00:10:51,500 Now what other data is there here, I mean date is there? 168 00:10:52,500 --> 00:10:55,000 22. So along with permissions, 169 00:10:55,000 --> 00:10:58,500 I want to assign this timestamp to my file. 170 00:10:59,900 --> 00:11:02,600 Then you have to use 'copystat'. 171 00:11:02,600 --> 00:11:07,800 [no audio] 172 00:11:07,800 --> 00:11:14,300 Simply 'shutil.copystat(src,dest)', 173 00:11:14,300 --> 00:11:18,300 [no audio] 174 00:11:18,300 --> 00:11:21,200 right? Now, let me run this and see the result. 175 00:11:21,200 --> 00:11:23,000 [no audio] 176 00:11:23,000 --> 00:11:26,000 Sorry, 'src'. Spelling mistake. 177 00:11:26,000 --> 00:11:30,100 [no audio] 178 00:11:30,100 --> 00:11:33,510 So, guys previously your backup file timestamp is this one. Now, 179 00:11:33,500 --> 00:11:37,900 we are assigning this timestamp to, I mean, this timestamp 180 00:11:37,900 --> 00:11:40,900 to your file. Now, just observe that. 181 00:11:42,400 --> 00:11:43,900 See that, timestamp of your 182 00:11:43,900 --> 00:11:49,210 backup file and your Python script are same, and permissions as well, 183 00:11:49,200 --> 00:11:54,300 so entire metadata will be assigned to your destination file. 184 00:11:54,600 --> 00:12:00,510 Make sure that you are not copying the content of your file to your destination file. 185 00:12:00,500 --> 00:12:03,900 You are giving only permissions. 186 00:12:03,900 --> 00:12:06,700 [no audio] 187 00:12:06,700 --> 00:12:10,400 That's it. See the content of this file is different. 188 00:12:10,400 --> 00:12:13,700 This is the content. And the content of this file is different, right? 189 00:12:13,700 --> 00:12:15,800 [no audio] 190 00:12:15,800 --> 00:12:18,300 This file is different. That's it. 191 00:12:18,300 --> 00:12:20,300 [no audio] 192 00:12:20,300 --> 00:12:26,100 That's fine. Now, yeah guys, this 'copyfileobj' is nothing but you 193 00:12:26,100 --> 00:12:30,900 know you can open a file using 'open' or 'read' mode, and you 194 00:12:30,900 --> 00:12:34,210 can write that content into some other file using 'write' mode. 195 00:12:34,200 --> 00:12:38,800 So, the file object whatever you created that file object 196 00:12:38,800 --> 00:12:41,700 can be copied from one file object to another file object using 197 00:12:41,700 --> 00:12:46,000 'copyfileobj'. Just assume that, you just derive it, this 198 00:12:46,000 --> 00:12:53,410 logic. Just suppose, assume that there is a file called 'xyz.txt', 199 00:12:53,400 --> 00:12:58,100 I'm opening that in 'read' mode, and I'm creating one more file object, 200 00:12:58,100 --> 00:13:02,100 [no audio] 201 00:13:02,100 --> 00:13:05,800 some 'pqr.txt' but this is in 'write' mode. 202 00:13:05,800 --> 00:13:07,800 [no audio] 203 00:13:07,800 --> 00:13:12,800 Right? Now if you want to copy the content of f1 to f2 then use 204 00:13:12,800 --> 00:13:23,000 'shutil.copyfileobj(f1,f2)'. That's it. You just try with this. 205 00:13:24,400 --> 00:13:28,110 Now, we are pending with one more operation that is 'copytree'. 206 00:13:28,100 --> 00:13:34,200 Nothing is there, 'copytree' is useful to clone or copy entire 207 00:13:34,200 --> 00:13:39,200 directory recursively to your destination. Let's say, suppose 208 00:13:39,200 --> 00:13:42,100 [no audio] 209 00:13:42,100 --> 00:13:43,700 I'm having some tomcat9. 210 00:13:43,700 --> 00:13:46,400 So, if you observe that, tomcat9, we are having 211 00:13:46,400 --> 00:13:48,600 different directory structure for that, 212 00:13:48,600 --> 00:13:51,710 I mean different files and folders in that, right? Now, 213 00:13:51,700 --> 00:13:54,500 what I want to do is, I want to copy that entire directory 214 00:13:54,500 --> 00:13:57,400 structure into some other file. Let's say 215 00:13:59,000 --> 00:14:03,700 'copytree'. Let me take now source as 216 00:14:04,400 --> 00:14:05,900 I'm going to create new source guys. 217 00:14:05,900 --> 00:14:14,510 [no audio] 218 00:14:14,500 --> 00:14:15,900 'tomcat7', suppose. 219 00:14:15,900 --> 00:14:18,400 [no audio] 220 00:14:18,400 --> 00:14:21,600 Now, 'src', then 'dest' directly I am giving 221 00:14:22,800 --> 00:14:24,600 under 'tmp', suppose 222 00:14:24,600 --> 00:14:27,400 [no audio] 223 00:14:27,400 --> 00:14:33,100 'tomcat7'. You can take any name, 'tomcat78', 224 00:14:33,100 --> 00:14:36,100 'tomcat', suppose some '7.8', something like that. 225 00:14:37,600 --> 00:14:39,500 Right now, let me run this. 226 00:14:39,500 --> 00:14:45,910 So before going to run, let me check it, is there any tomcat9? 227 00:14:45,900 --> 00:14:49,910 I mean something 'tomcat7', or simply 'tomcat'. That is your wish. 228 00:14:49,900 --> 00:14:52,510 You can take any destination directory. 229 00:14:52,500 --> 00:14:54,100 We don't have any tomcat here. Now 230 00:14:54,100 --> 00:14:56,800 let me run our Python script. 231 00:14:58,400 --> 00:15:04,100 Sorry. 'src', I missed the '/' here. 232 00:15:05,400 --> 00:15:06,400 It's done. 233 00:15:06,400 --> 00:15:09,100 [no audio] 234 00:15:09,100 --> 00:15:10,400 tomcat has been created. 235 00:15:10,400 --> 00:15:12,400 [no audio] 236 00:15:12,400 --> 00:15:18,800 And make sure that it is giving entire metadata, whatever 237 00:15:18,800 --> 00:15:22,700 you are having for your source that metadata will be provided 238 00:15:22,700 --> 00:15:27,600 for your destination as well, using 'copytree' operation from 239 00:15:27,600 --> 00:15:29,400 your Shutil Module. That's it. 240 00:15:30,400 --> 00:15:37,300 Right. Your last point, suppose guys if you want 241 00:15:37,300 --> 00:15:41,300 to remove, if you want to remove a directory structure 242 00:15:41,300 --> 00:15:44,100 recursively, then simply you can use 243 00:15:44,100 --> 00:15:46,510 [no audio] 244 00:15:46,500 --> 00:15:51,200 'shutil.rmtree', simply your destination. 245 00:15:51,200 --> 00:15:54,000 So, I am going to remove this path only. Directly 246 00:15:54,000 --> 00:15:57,300 you can provide your destination or you can store your 247 00:15:57,300 --> 00:16:03,010 destination into some variable, some 'dest' variable, and then 248 00:16:03,000 --> 00:16:05,200 you can use that variable, otherwise directly 249 00:16:05,200 --> 00:16:09,810 you can provide your source and destination paths in your 250 00:16:09,800 --> 00:16:15,400 operation itself, so 'rmtree'. So it will remove the entire 251 00:16:15,400 --> 00:16:17,600 directory structure recursively. 252 00:16:17,600 --> 00:16:20,400 So, suppose if I run that. 253 00:16:21,900 --> 00:16:28,310 And see the 'tmp' location, you don't have tomcat here. That's it. So guys, 254 00:16:28,300 --> 00:16:36,110 these are the simple copy file operations with your Shutil Module, okay. Very useful 255 00:16:36,100 --> 00:16:40,700 your devop site, or some Linux admin site, or maybe 256 00:16:40,700 --> 00:16:44,200 any admin, like WebLogic or database because you are going 257 00:16:44,200 --> 00:16:49,500 to take some backups, right, and in your database site, 258 00:16:49,500 --> 00:16:53,810 sometimes you need to create same timestamp for your destination as well. 259 00:16:53,800 --> 00:16:59,310 So, at that time you can use simply 'copy2' operation from your Shutil Module, 260 00:16:59,300 --> 00:17:02,200 okay. Okay guys, thank you for watching this video.