1 00:00:00,000 --> 00:00:03,690 In Python, or in any other language, you get the 2 00:00:03,690 --> 00:00:07,110 core set of functions. For example, the print 3 00:00:07,110 --> 00:00:09,990 function and the open functions are part of the 4 00:00:09,990 --> 00:00:13,560 core Python functions. You can just use them like 5 00:00:13,560 --> 00:00:16,440 that, and it's going to work. Now you have many 6 00:00:16,440 --> 00:00:19,290 built in functions. But in the end, what you can 7 00:00:19,290 --> 00:00:22,110 do with them is quite limited. If you want to do 8 00:00:22,110 --> 00:00:24,540 more things, for example, you want to create or 9 00:00:24,540 --> 00:00:27,570 you want to remove a folder, connect to a web 10 00:00:27,570 --> 00:00:32,100 page, send an email, etc, etc, you need to write a 11 00:00:32,100 --> 00:00:35,040 lot of code all by yourself. And this is going to 12 00:00:35,040 --> 00:00:38,460 take you a long time. Fortunately for you, there 13 00:00:38,460 --> 00:00:42,000 are many available libraries of Python modules 14 00:00:42,000 --> 00:00:45,090 that you can use. The Python module is simply a 15 00:00:45,090 --> 00:00:48,330 collection of plug and play functionalities to 16 00:00:48,330 --> 00:00:52,200 solve a specific problem. And there are so many 17 00:00:52,200 --> 00:00:55,470 Python modules that, of course, I won't show them 18 00:00:55,500 --> 00:00:58,890 all, and you won't be able to know them all. But 19 00:00:58,890 --> 00:01:01,260 here, I'm going to show you how you can import a 20 00:01:01,260 --> 00:01:04,590 module and use its functionalities in your Python 21 00:01:04,590 --> 00:01:07,680 code. And then you will be able to replicate this 22 00:01:07,710 --> 00:01:11,430 for each new module you want to use. And first, 23 00:01:11,430 --> 00:01:14,850 I'm going to also comment this. So now I have a 24 00:01:14,850 --> 00:01:17,820 lot of commented line. So this is just as if the 25 00:01:17,850 --> 00:01:22,523 file was empty. I'm going to simply do import os. 26 00:01:22,523 --> 00:01:26,760 And I'm going to run that. And you can see 27 00:01:26,790 --> 00:01:29,220 this is working. So we don't have any error. But 28 00:01:29,220 --> 00:01:31,830 we also don't have anything on the screen. Okay, 29 00:01:32,190 --> 00:01:36,060 so to import a module, you simply do import, and 30 00:01:36,060 --> 00:01:38,700 then the name of the module, and then you can use 31 00:01:38,700 --> 00:01:41,670 the module in your code. And best practice is, of 32 00:01:41,670 --> 00:01:44,040 course, to put it at the beginning of your 33 00:01:44,040 --> 00:01:47,820 program. So I'm going to put it right there. Okay, 34 00:01:47,820 --> 00:01:50,850 even if I have commented lines, I'm going to put 35 00:01:50,850 --> 00:01:52,980 it at the beginning of the program. In the os 36 00:01:52,980 --> 00:01:56,220 module, what is the os module. The os module is 37 00:01:56,220 --> 00:01:59,250 simply going to give you some new functionality to 38 00:01:59,280 --> 00:02:02,400 interact with the os, and for example, to create 39 00:02:02,400 --> 00:02:06,750 files, remove files, create directories, etc. And 40 00:02:06,750 --> 00:02:09,720 now let's use this module, so this os module to do 41 00:02:09,720 --> 00:02:12,780 something useful. Let's say we want know, to 42 00:02:12,780 --> 00:02:15,870 remove the new file here that we have created 43 00:02:15,870 --> 00:02:19,290 previously. How to remove that? What we can simply 44 00:02:19,290 --> 00:02:25,223 do is, I'm going to write the code if os.path.exists, 45 00:02:25,223 --> 00:02:27,690 we are first going to check if the 46 00:02:27,810 --> 00:02:31,994 file exists actually, before we remove it. new_file, 47 00:02:31,994 --> 00:02:38,220 I'm going to do os.remove with new_file. 48 00:02:38,220 --> 00:02:42,120 So this is a function from os and actually, 49 00:02:42,120 --> 00:02:47,044 you have a sub module named path. So os.path.exists 50 00:02:47,044 --> 00:02:49,200 is going to tell you if the file 51 00:02:49,230 --> 00:02:52,440 exists and return a Boolean. So if this is true, 52 00:02:52,680 --> 00:02:55,200 you go inside the if and then you have a function, 53 00:02:55,230 --> 00:02:57,930 os.remove, which is simply going to remove 54 00:02:57,930 --> 00:03:01,950 a file. Here, you can give the relative path like 55 00:03:01,950 --> 00:03:05,970 this or the absolute path like we did before. And 56 00:03:05,970 --> 00:03:08,490 because we use that multiple times, I can also 57 00:03:08,490 --> 00:03:12,837 create a variable, let's say file_name is equal to new_file 58 00:03:12,837 --> 00:03:16,830 and just pass that variable here, which is 59 00:03:16,830 --> 00:03:20,340 going to make it more convenient. Now let's run 60 00:03:20,340 --> 00:03:25,140 that code, and let's look at the file here. Okay, 61 00:03:25,200 --> 00:03:29,250 the file is gone, new_file is gone. Now if I run 62 00:03:29,250 --> 00:03:31,920 it again, you can see so the file is still gone. 63 00:03:31,920 --> 00:03:34,560 And we don't have any error because we first check 64 00:03:34,560 --> 00:03:38,100 if the file exists before we try to remove it. If 65 00:03:38,100 --> 00:03:41,070 you try to remove a file that doesn't exist, you 66 00:03:41,070 --> 00:03:43,740 are going to get an error. So well, as you can see 67 00:03:43,740 --> 00:03:47,280 that just the way of using a module. import name 68 00:03:47,280 --> 00:03:50,730 of the module, and then you can use module dot 69 00:03:51,060 --> 00:03:53,820 name of the function. And sometimes you have a 70 00:03:53,820 --> 00:03:57,270 module inside a module. So module, so module 71 00:03:57,390 --> 00:04:00,360 function, alright, and one thing you can also do 72 00:04:00,360 --> 00:04:03,540 is instead of using import os, okay, which is 73 00:04:03,540 --> 00:04:06,690 going to import all the os module, you can import 74 00:04:06,690 --> 00:04:10,800 specific functions. So if you want to do that, you 75 00:04:10,800 --> 00:04:16,290 can do from os import, for example, remove. This 76 00:04:16,290 --> 00:04:19,740 is going to import the remove function from the os 77 00:04:19,740 --> 00:04:26,161 module. And you can do from os.path import exist. 78 00:04:26,970 --> 00:04:29,880 Okay, I'm going to remove this. And now what you 79 00:04:29,880 --> 00:04:33,240 can do, you can simply use the exists and remove 80 00:04:33,330 --> 00:04:36,690 functions like this. So if you directly import the 81 00:04:36,690 --> 00:04:40,367 module like this. import os, you need to put os.remove, 82 00:04:40,367 --> 00:04:43,380 but if you do from os import remove, 83 00:04:43,380 --> 00:04:46,290 you simply need to just write remove like this, 84 00:04:46,320 --> 00:04:49,170 and this is also going to work and with this way 85 00:04:49,170 --> 00:04:52,050 you simply import two functions. You're gonna 86 00:04:52,170 --> 00:04:55,200 import any other functions from the os module, 87 00:04:55,380 --> 00:04:59,160 just what you need. And now well, basically how to 88 00:04:59,160 --> 00:05:01,440 find that information, how to know that you need 89 00:05:01,440 --> 00:05:05,970 to do os.path.exists, os.remove? Well, 90 00:05:06,240 --> 00:05:08,490 I'm going to say to you, you need to search that 91 00:05:08,520 --> 00:05:10,920 on Google. And that's what I do all the time, 92 00:05:10,920 --> 00:05:14,340 that's what every Python developer do all the time 93 00:05:14,370 --> 00:05:17,400 is to search on Google. And to easily search for a 94 00:05:17,400 --> 00:05:21,150 module, you simply type, Python, the name of the 95 00:05:21,150 --> 00:05:23,520 module, and then module, and you're going to get 96 00:05:23,550 --> 00:05:29,702 to page so you can get to that page, docs.python.org. 97 00:05:29,702 --> 00:05:32,010 Okay, and on the top, so you can even 98 00:05:32,010 --> 00:05:34,890 choose the language and make sure that you have a 99 00:05:34,890 --> 00:05:38,130 version that is at least Python three, and that 100 00:05:38,130 --> 00:05:40,560 corresponds to the version you are using, okay? 101 00:05:40,770 --> 00:05:42,330 And then you can see you have the complete 102 00:05:42,360 --> 00:05:46,860 documentation for os. Okay, so if I search for 103 00:05:46,860 --> 00:05:50,760 os.remove, you can see I have always let's 104 00:05:50,760 --> 00:05:53,730 remove here, and you have all the information with 105 00:05:53,730 --> 00:05:56,760 some examples, sometimes the parameters you need 106 00:05:56,760 --> 00:06:00,420 to give, etc, etc. Alright, now you know how to 107 00:06:00,420 --> 00:06:03,360 import and to use a python module in your program. 108 00:06:03,450 --> 00:06:05,910 And you also know where you can find a complete 109 00:06:05,910 --> 00:06:09,390 documentation for any given built in module. And 110 00:06:09,390 --> 00:06:11,250 of course, you're not going to know all the 111 00:06:11,250 --> 00:06:13,830 modules and all the functions that whenever you 112 00:06:13,830 --> 00:06:16,650 have a problem, you just make a Google search and 113 00:06:16,650 --> 00:06:18,720 you're going to see what kind of module you need 114 00:06:18,720 --> 00:06:21,255 to use what kind of functions then you can read to 115 00:06:21,630 --> 00:06:24,490 the condition so you can use that correctly.