1 00:00:00,000 --> 00:00:00,900 [no audio] 2 00:00:00,900 --> 00:00:05,600 Friends here, we are going to see how to copy a file into another file 3 00:00:05,600 --> 00:00:10,200 using Python, or simply copy the content of a file to another 4 00:00:10,200 --> 00:00:15,700 file. Let us assume that you have a file with some content 5 00:00:16,300 --> 00:00:19,700 in one location, in any location in your host, 6 00:00:21,100 --> 00:00:24,100 and whatever the data you have in that file that data 7 00:00:24,100 --> 00:00:28,100 I want to copy to some another file which is there in maybe 8 00:00:28,100 --> 00:00:29,800 same location or in different location. 9 00:00:30,700 --> 00:00:35,400 So this entire data I want to copy into this file. Assume 10 00:00:35,400 --> 00:00:38,200 that this is a source file and this is a destination file. 11 00:00:39,700 --> 00:00:43,000 So what you are doing guys? Simply the content which is there 12 00:00:43,000 --> 00:00:46,100 in your source file, that content you're going to write it 13 00:00:46,100 --> 00:00:47,300 into destination file. 14 00:00:47,300 --> 00:00:49,100 [no audio] 15 00:00:49,100 --> 00:00:52,500 Now first step what you have to do? Read the content of your 16 00:00:52,500 --> 00:00:57,800 source file, then whatever the data you read from your source 17 00:00:57,800 --> 00:01:01,400 file that data just write it into destination file. 18 00:01:02,600 --> 00:01:06,500 You know, if you want to read a data from any file in your 19 00:01:06,500 --> 00:01:09,000 Python you have to open that file in 'read' mode. 20 00:01:10,000 --> 00:01:12,700 And if you want to write any content to a file, then you 21 00:01:12,700 --> 00:01:15,200 have to open that file in 'write' mode. 22 00:01:15,200 --> 00:01:17,000 [no audio] 23 00:01:17,000 --> 00:01:20,700 'read' mode, you can able to read. 'write' mode, the content which 24 00:01:20,700 --> 00:01:23,700 you have in your hand that content you can write it into 25 00:01:23,700 --> 00:01:26,200 your file. That's it. 26 00:01:27,300 --> 00:01:31,200 So, let me open my editor just to write your Python Script. 27 00:01:32,200 --> 00:01:34,800 So what I am doing is simply I am going to save 28 00:01:34,800 --> 00:01:36,600 a Python Script name as 29 00:01:36,600 --> 00:01:39,000 [no audio] 30 00:01:39,000 --> 00:01:40,800 'source_to_destination.py'. 31 00:01:42,400 --> 00:01:43,400 So let me take 32 00:01:43,400 --> 00:01:44,900 first of all source file. 33 00:01:45,300 --> 00:01:47,000 You can take any file. As of now 34 00:01:47,000 --> 00:01:48,800 let me take my file as, 35 00:01:50,200 --> 00:01:51,200 let's say 36 00:01:51,200 --> 00:01:55,000 [no audio] 37 00:01:55,000 --> 00:01:56,800 Yeah, I have somewhere 38 00:01:56,800 --> 00:01:58,200 file. Let me take that. 39 00:01:59,400 --> 00:02:00,500 Suppose if you observe here 40 00:02:00,500 --> 00:02:02,500 I have a file called 'random'. In that 41 00:02:02,500 --> 00:02:03,600 I have some three lines. 42 00:02:04,200 --> 00:02:05,800 Let me take this file name first. 43 00:02:05,800 --> 00:02:08,600 [no audio] 44 00:02:08,600 --> 00:02:11,000 'random.txt', and I want 45 00:02:11,000 --> 00:02:14,900 to copy this content to some destination file. 46 00:02:14,900 --> 00:02:21,600 Let's say destination file name equals to 'newrandom.txt'. 47 00:02:23,000 --> 00:02:26,700 So first of all source file content you have to read, 48 00:02:27,300 --> 00:02:29,700 then whatever the data you are reading from source file 49 00:02:29,700 --> 00:02:32,000 that data we need to write into destination file. 50 00:02:32,700 --> 00:02:36,400 So what I am doing is, let me read the content 51 00:02:36,400 --> 00:02:37,700 from your source file first. 52 00:02:38,800 --> 00:02:42,900 So 'sfo=', open that source file in 53 00:02:42,900 --> 00:02:46,000 [no audio] 54 00:02:46,000 --> 00:02:51,800 'r' mode, then let me close your opened file. 55 00:02:51,800 --> 00:02:56,400 'sfo.close'. Let me save it and run it. 56 00:02:56,400 --> 00:02:58,200 [no audio] 57 00:02:58,200 --> 00:03:01,700 You're not getting anything. You opened your file in 'r' mode, 58 00:03:01,800 --> 00:03:02,900 but where is your data? 59 00:03:03,000 --> 00:03:04,500 See if you want to see the data, 60 00:03:04,500 --> 00:03:08,000 if you want to read the data from your source file, you know 61 00:03:09,000 --> 00:03:13,300 any variable, or let me say 'content=', your 62 00:03:13,300 --> 00:03:17,500 'sfo.read', read entire data as a string. 63 00:03:18,800 --> 00:03:21,100 You know you have different operations 'read', 'readline', 64 00:03:21,100 --> 00:03:25,000 'readlines'. So 'read' is a string, 'readline' is a string, 65 00:03:25,000 --> 00:03:26,400 'readlines' is a list. 66 00:03:26,800 --> 00:03:29,000 So I am going to read entire data as a string. 67 00:03:29,200 --> 00:03:30,300 Yes, you can read it. 68 00:03:31,100 --> 00:03:33,300 Now, let me print the data from your source file, 69 00:03:33,400 --> 00:03:34,800 whatever the data you are reading. 70 00:03:34,800 --> 00:03:37,500 [no audio] 71 00:03:37,500 --> 00:03:40,900 Yes, that's it. But this data I want to write it into 72 00:03:40,900 --> 00:03:42,200 my destination file. 73 00:03:42,300 --> 00:03:45,100 Nothing is there. It's very simple. If you want to write any 74 00:03:45,100 --> 00:03:47,000 data into a file what you have to do? 75 00:03:48,500 --> 00:03:49,800 Just open that file. 76 00:03:50,000 --> 00:03:54,700 Let me take this 'dfo= open(, and then 77 00:03:55,000 --> 00:03:56,400 destination file. Guys, 78 00:03:56,400 --> 00:03:59,700 our file name is already there in this variable. Then 'w' 79 00:03:59,700 --> 00:04:02,000 mode, then close that file. 80 00:04:02,000 --> 00:04:06,400 [no audio] 81 00:04:06,400 --> 00:04:09,400 But before going to close you have to write the content in 82 00:04:09,400 --> 00:04:10,300 your destination file. 83 00:04:10,300 --> 00:04:15,300 That's why, your 'dfo.write', already you have in 84 00:04:15,300 --> 00:04:18,500 your hand the content of your source file, that is with 85 00:04:18,500 --> 00:04:21,200 the variable called 'content'. Save it. 86 00:04:22,500 --> 00:04:26,100 Done. Let me run it. Done. 87 00:04:26,800 --> 00:04:30,500 Let me see that there is a 'newrandom' file in our location. 88 00:04:30,500 --> 00:04:32,300 [no audio] 89 00:04:32,300 --> 00:04:33,500 Let me search with 'n'. 90 00:04:33,500 --> 00:04:36,300 [no audio] 91 00:04:36,300 --> 00:04:39,600 Yeah, 'newrandom' is there. That's it. 92 00:04:41,300 --> 00:04:43,300 Right, and one more thing guys. 93 00:04:43,300 --> 00:04:45,800 Suppose this 'random.txt' 94 00:04:46,000 --> 00:04:49,000 I am copying into Desktop location. 95 00:04:49,000 --> 00:04:52,000 [no audio] 96 00:04:52,000 --> 00:04:53,100 Right. Fine. 97 00:04:53,600 --> 00:04:57,500 Now, if you observe your Python Script, this Python Script is there 98 00:04:57,500 --> 00:05:00,600 in suppose some different location. 'PythonScripts' directory 99 00:05:00,600 --> 00:05:04,200 you have your Python Script. And I'm running. See that. What 100 00:05:04,200 --> 00:05:07,500 you are getting? You're getting an error like, while opening 101 00:05:07,500 --> 00:05:09,600 your source file, you are getting an error like 102 00:05:09,600 --> 00:05:11,200 'No such file or directory'. 103 00:05:11,200 --> 00:05:13,800 The reason is your Python Script 104 00:05:13,800 --> 00:05:17,800 is there somewhere in 'PythonScripts' directory. Under Desktop 105 00:05:17,800 --> 00:05:19,900 you have a directory called 'PythonScripts', under that you 106 00:05:19,900 --> 00:05:23,600 have your Python Script. But your 'random.txt' is not 107 00:05:23,600 --> 00:05:28,000 there in the location where you have your Python Script. That 108 00:05:28,000 --> 00:05:30,400 is there in a different location called Desktop. 109 00:05:31,100 --> 00:05:34,400 So that's why whenever if you are trying to read or write, 110 00:05:34,600 --> 00:05:37,900 or whenever if you are working with any files try to provide 111 00:05:37,900 --> 00:05:38,900 complete path. 112 00:05:40,100 --> 00:05:41,300 Right. See, now 113 00:05:41,300 --> 00:05:44,700 what I am doing is, I am going to take complete path. Let's say 114 00:05:44,700 --> 00:05:46,900 [no audio] 115 00:05:46,900 --> 00:05:48,200 I need 'Desktop' path. 116 00:05:48,200 --> 00:05:51,400 [no audio] 117 00:05:51,400 --> 00:05:52,800 So guys, this is Windows, 118 00:05:52,800 --> 00:05:56,100 that's why whenever if you are giving a path, please provide 119 00:05:56,200 --> 00:05:57,700 '\\' in your path. 120 00:05:57,700 --> 00:06:01,700 [no audio] 121 00:06:01,700 --> 00:06:02,700 That's it. 122 00:06:04,100 --> 00:06:07,000 Now destination file location simply you are giving file name, 123 00:06:07,500 --> 00:06:09,100 but where you want to store exactly? 124 00:06:10,600 --> 00:06:14,400 If you don't provide any complete path, then the destination 125 00:06:14,400 --> 00:06:18,100 file will create in the location of where your Python Script 126 00:06:18,100 --> 00:06:21,200 is there. From which location you are running your Python 127 00:06:21,200 --> 00:06:24,500 Script, in that location only it will create. Now 128 00:06:24,500 --> 00:06:25,900 I want to create my Python, 129 00:06:25,900 --> 00:06:29,200 I mean my destination file in a different location called, 130 00:06:29,200 --> 00:06:30,500 suppose 'Downloads'. 131 00:06:31,700 --> 00:06:36,000 Right. So I want to create my destination file called 'Downloads' 132 00:06:36,000 --> 00:06:38,200 location. Let's say, copy that. 133 00:06:38,200 --> 00:06:40,300 [no audio] 134 00:06:40,300 --> 00:06:43,200 Provide the complete path, 'Downloads'. 135 00:06:43,300 --> 00:06:47,700 So be clear, whenever if you are working with files path, 136 00:06:48,200 --> 00:06:51,500 you have to provide '\\' in your file path in your 137 00:06:51,500 --> 00:06:56,000 Windows. Now, let me save it and before going to run 138 00:06:56,100 --> 00:06:57,400 observe that in Desktop 139 00:06:57,400 --> 00:07:01,000 you don't have any file called 'newrandom.txt'. Now, 140 00:07:01,000 --> 00:07:02,800 I am going to save it and run it. 141 00:07:02,800 --> 00:07:04,700 [no audio] 142 00:07:04,700 --> 00:07:06,700 Python Script has completed. Observe that 143 00:07:06,700 --> 00:07:08,800 There is a file created 'newrandom'. 144 00:07:09,800 --> 00:07:10,800 That's it. 145 00:07:11,100 --> 00:07:14,000 And as of now whatever the Python Script you have in your 146 00:07:14,000 --> 00:07:18,300 hand that is always going to work with fixed file, 'random', 147 00:07:18,700 --> 00:07:20,800 and then this is going to create a destination file called 148 00:07:20,800 --> 00:07:25,100 'newrandom.txt'. But I don't want to do in this way. Whenever 149 00:07:25,100 --> 00:07:28,200 if I run my Python Script it has to ask, "Enter your source 150 00:07:28,200 --> 00:07:31,000 file", "Enter the destination file", then that's good. 151 00:07:31,900 --> 00:07:34,600 So that's why what I am doing is, simply I am going to comment 152 00:07:34,600 --> 00:07:37,300 these two lines. Now instead of these two lines 153 00:07:37,300 --> 00:07:40,600 what I am writing is 'sfile=', I want to read 154 00:07:40,600 --> 00:07:45,100 the source file location while running your script. "Enter 155 00:07:45,100 --> 00:07:48,200 your source file location or source file". 156 00:07:49,800 --> 00:07:51,800 Now this script is like a dynamic script. 157 00:07:51,900 --> 00:07:58,300 It will work on any required files, right. "Enter your destination. 158 00:08:00,000 --> 00:08:02,100 file". That's it. Save it. 159 00:08:02,600 --> 00:08:06,800 Go to command line. Guys, in Sublime Text you can't provide input, 160 00:08:06,800 --> 00:08:08,800 that's why I'm going to run my Python Script from, 161 00:08:08,800 --> 00:08:11,000 [no audio] 162 00:08:11,000 --> 00:08:13,900 let's say we have our Python Script somewhere here. 163 00:08:14,000 --> 00:08:17,400 Our Python Script is source to destination, 's_d.py'. 164 00:08:18,300 --> 00:08:19,700 Now, I am going to run this. 165 00:08:19,700 --> 00:08:22,100 [no audio] 166 00:08:22,100 --> 00:08:27,500 So what I am doing is I want to copy our Python Script itself. 167 00:08:27,800 --> 00:08:32,200 Just assume that. I want to copy our Python Script itself 168 00:08:32,200 --> 00:08:35,700 [no audio] 169 00:08:35,700 --> 00:08:37,100 into some different location. 170 00:08:37,100 --> 00:08:40,100 [no audio] 171 00:08:40,100 --> 00:08:42,600 Let me copy complete path then. 172 00:08:45,000 --> 00:08:48,400 So in this location we have our Python Script, and 173 00:08:48,400 --> 00:08:49,799 let me provide '\\ 174 00:08:49,799 --> 00:09:00,600 [no audio] 175 00:09:00,600 --> 00:09:07,400 PythonScripts\\s_d.py'. This file 176 00:09:07,400 --> 00:09:11,200 I need to copy, this file content 177 00:09:11,300 --> 00:09:15,100 I need to copy into some different location with same name 178 00:09:15,100 --> 00:09:17,000 or different name, that is your wish. 179 00:09:17,400 --> 00:09:21,300 Let's say I want to move this into suppose for 'Desktop' location. 180 00:09:21,800 --> 00:09:26,000 I want to copy this file into 'Desktop' location. In the 'Desktop' 181 00:09:26,000 --> 00:09:27,800 I want to provide same python script name, 182 00:09:27,800 --> 00:09:30,300 yes you can, or with some different name 183 00:09:30,300 --> 00:09:34,800 also you can provide. Just let's say I am taking same file 184 00:09:34,800 --> 00:09:37,200 name. Okay. 'Enter'. Done. 185 00:09:37,900 --> 00:09:41,500 Go to your 'Desktop', now search for 's_d'. 186 00:09:41,500 --> 00:09:44,900 Yes, it has been created. Open that file and see the content. 187 00:09:44,900 --> 00:09:47,500 [no audio] 188 00:09:47,500 --> 00:09:52,700 You got it. Right. So simply this is a Python Script just 189 00:09:52,700 --> 00:09:56,000 to copy source file content into destination file. 190 00:09:57,500 --> 00:10:02,400 Right. And guys you may get doubt, suppose if you go with Unix 191 00:10:02,400 --> 00:10:06,100 or Windows there are some commands like 'cp source 192 00:10:06,100 --> 00:10:08,000 [no audio] 193 00:10:08,000 --> 00:10:11,500 destination'. Single line code is enough, 194 00:10:11,500 --> 00:10:14,400 right. But why we are writing this many number of lines 195 00:10:15,700 --> 00:10:18,500 to copy your source content to destination file? 196 00:10:18,500 --> 00:10:20,200 [no audio] 197 00:10:20,200 --> 00:10:23,000 Right. So as of now, there is no much usage, 198 00:10:23,000 --> 00:10:26,100 but while going forward this concept is very, very helpful, 199 00:10:26,100 --> 00:10:27,200 very, very useful. 200 00:10:27,700 --> 00:10:31,100 So that's why just I have given introduction to copy one 201 00:10:31,100 --> 00:10:35,500 file content into another file content, right, here. And one 202 00:10:35,500 --> 00:10:36,800 more thing, last point. 203 00:10:38,200 --> 00:10:42,000 So by default guys, by default, if you don't open your file 204 00:10:42,000 --> 00:10:45,400 in read mode, I mean if you don't provide any mode while 205 00:10:45,400 --> 00:10:48,600 opening your file, by default that is the read mode. If you 206 00:10:48,600 --> 00:10:50,700 want to verify, let's say 'print()', 207 00:10:52,400 --> 00:10:54,100 your 'sfo., 208 00:10:54,700 --> 00:10:56,900 what is the mode for that. Right. 209 00:10:57,100 --> 00:11:00,800 Now, what I am doing is, I am going to just run here itself. 210 00:11:01,400 --> 00:11:04,500 So I'm going to provide fixed filenames, observe the output. 211 00:11:04,700 --> 00:11:09,900 You're getting 'r'. 'r', that means if you open your file without 212 00:11:09,900 --> 00:11:13,700 mentioning any mode, by default that is the 'read' mode. That's it. 213 00:11:13,700 --> 00:11:16,900 [no audio] 214 00:11:16,900 --> 00:11:18,600 Right. Okay. 215 00:11:18,800 --> 00:11:21,100 Okay guys, thank you for watching this video. While going 216 00:11:21,100 --> 00:11:23,900 forward we will use this concept to work with some real time 217 00:11:23,900 --> 00:11:25,200 scenarios. Okay. 218 00:11:25,200 --> 00:11:32,736 [no audio]