1 00:00:00,000 --> 00:00:01,300 [no audio] 2 00:00:01,300 --> 00:00:03,200 Friends, here we are going to do 3 00:00:03,200 --> 00:00:06,200 simple practice with your platform and 4 00:00:06,200 --> 00:00:13,100 'os' modules. Task is, write a single Python script to clear 5 00:00:13,200 --> 00:00:18,500 terminal of any operating system or I can say write a platform 6 00:00:18,500 --> 00:00:20,700 independent script to clear terminal. 7 00:00:21,900 --> 00:00:22,900 See here, 8 00:00:23,600 --> 00:00:26,900 my intention is I want to write one Python script, and that 9 00:00:26,900 --> 00:00:30,200 Python script has to run on any of your operating system, 10 00:00:30,400 --> 00:00:33,600 and while running that script on your any one of your 11 00:00:33,600 --> 00:00:34,600 operating systems 12 00:00:34,600 --> 00:00:37,900 it has to clear the terminal. That means first 13 00:00:37,900 --> 00:00:41,200 let me open my Windows operating system. Suppose 14 00:00:41,200 --> 00:00:44,100 I am running in this way. Now clear screen means, suppose if 15 00:00:44,100 --> 00:00:48,400 I run here Python script it has to do in this way, 16 00:00:48,500 --> 00:00:52,600 it has to clear my terminal. Same way suppose 17 00:00:53,000 --> 00:00:57,600 I am, this is my Unix or Linux system, right, if I run your 18 00:00:57,600 --> 00:00:58,800 Python script here, 19 00:00:58,800 --> 00:01:01,500 it has to clear a screen, it has to do in this way. 20 00:01:02,200 --> 00:01:04,400 So in that way I need to implement my script. 21 00:01:05,900 --> 00:01:12,200 See guys, by default to clear your terminal suppose as of 22 00:01:12,200 --> 00:01:14,300 now based on our previous concept, suppose 23 00:01:14,300 --> 00:01:17,900 we don't have any, we didn't see any command in Python directly, 24 00:01:18,800 --> 00:01:24,600 but we know one thing, from 'os' module we can run any operating 25 00:01:24,600 --> 00:01:27,100 system command, right. That's why 26 00:01:27,100 --> 00:01:30,200 what I am doing is, I am writing a simple Python script, right, 27 00:01:30,200 --> 00:01:32,800 that is 'clear_screen'. 28 00:01:33,900 --> 00:01:36,400 Guys, directly you can run 'clear' command on your Unix-like 29 00:01:36,400 --> 00:01:39,400 systems, and the 'cls' command on your Windows operating system, 30 00:01:39,400 --> 00:01:44,900 but to provide some idea to use your different modules, to 31 00:01:44,900 --> 00:01:46,100 work with your different modules 32 00:01:46,100 --> 00:01:52,100 I am taking this simple example, right, 'clear_screen.py'. 33 00:01:52,100 --> 00:01:57,700 See I can import, I can import 'os' module, right? 34 00:01:58,000 --> 00:01:59,700 See if it is Unix-like system, 35 00:01:59,700 --> 00:02:03,000 I have to run 'os.system("clear")' command, 36 00:02:04,000 --> 00:02:08,900 and if it is Windows I have to run 'cls' command. See my intention 37 00:02:08,900 --> 00:02:13,699 is, I want to write one Python script, and it has to work with 38 00:02:13,800 --> 00:02:18,800 all operating systems. Suppose if I take only 'os.system()', 39 00:02:18,800 --> 00:02:19,800 and then 'clear', 40 00:02:21,200 --> 00:02:24,300 right, then it will work with only your Unix-like systems. 41 00:02:24,300 --> 00:02:30,200 Let me run this 'python3', or first of all randomly, right. 42 00:02:30,200 --> 00:02:32,000 [no audio] 43 00:02:32,000 --> 00:02:36,300 Now, I am going to run your script. 'python3', then 'clear_screen'. 44 00:02:36,600 --> 00:02:39,300 Yes, it's clearing your screen. But same script 45 00:02:39,300 --> 00:02:40,400 I want to copy, 46 00:02:40,400 --> 00:02:42,600 [no audio] 47 00:02:42,600 --> 00:02:44,300 to your Windows operating system. There 48 00:02:44,300 --> 00:02:46,600 I want to run. Let me do it one thing. 49 00:02:46,700 --> 00:02:52,400 'vim clear_screen.py'. So here 50 00:02:52,400 --> 00:02:54,900 I am going to copy whatever the code you are having here. 51 00:02:54,900 --> 00:02:56,000 Let me copy that code. 52 00:02:56,500 --> 00:03:00,300 So this code I am copying, and I'm going to paste it on 53 00:03:00,300 --> 00:03:01,300 your windows. 54 00:03:01,300 --> 00:03:03,400 [no audio] 55 00:03:03,400 --> 00:03:08,000 Right. Now, see that. Let me save it and let me run it. 56 00:03:09,400 --> 00:03:12,500 'python', on Windows you know 'python' is there, 57 00:03:13,500 --> 00:03:14,500 'clear_screen', 58 00:03:15,500 --> 00:03:18,100 See you are getting an error, 'clear' is not recognized as 59 00:03:18,100 --> 00:03:23,000 an internal or external command, because 'clear' command 60 00:03:23,000 --> 00:03:25,300 is for your Unix-like systems, not for Windows. 61 00:03:26,700 --> 00:03:30,500 Now what I have to do? Whenever if you're running from Windows, 62 00:03:31,100 --> 00:03:35,000 right, I need to run 'cls' instead of 'clear', 63 00:03:36,500 --> 00:03:42,300 right, but because of this modification you have two different 64 00:03:42,300 --> 00:03:49,100 scripts. See this is 'os.system("cls")', but this is 'os.system("clear")'. 65 00:03:49,300 --> 00:03:53,100 But what I have to do is, I have to run same script on 66 00:03:53,100 --> 00:03:56,500 Windows as well as your other Unix-like systems so that it 67 00:03:56,500 --> 00:04:01,200 has to work. Then see that, first of all I am writing, 68 00:04:02,500 --> 00:04:03,500 Sorry. 69 00:04:03,500 --> 00:04:08,400 [no audio] 70 00:04:08,400 --> 00:04:09,900 I am writing both the commands here. 71 00:04:10,300 --> 00:04:11,400 'os.system( 72 00:04:11,400 --> 00:04:14,200 [no audio] 73 00:04:14,200 --> 00:04:19,700 "clear")', and then 'os.system("cls")', but I want to 74 00:04:19,700 --> 00:04:23,100 run this command if it is Unix-like systems, 75 00:04:23,399 --> 00:04:26,600 I want to run this command if it is Windows operating system, 76 00:04:26,899 --> 00:04:27,899 right. Suppose first 77 00:04:27,899 --> 00:04:28,900 I will take 'cls', 78 00:04:30,100 --> 00:04:32,300 then second I will take 'clear'. 79 00:04:33,400 --> 00:04:34,700 See guys, if you remember 80 00:04:36,000 --> 00:04:39,500 you can identify your operating system name from this module, 81 00:04:39,600 --> 00:04:41,800 'platform' module, right? 82 00:04:41,800 --> 00:04:45,200 See what I am doing is, 'if platform.', 83 00:04:46,500 --> 00:04:49,500 'windows', sorry, 'system', 84 00:04:49,500 --> 00:04:51,700 [no audio] 85 00:04:51,700 --> 00:04:57,600 if it is '== "Windows"', then I am going to run 86 00:04:57,600 --> 00:05:01,300 'os.system("cls")', 'else' 87 00:05:02,000 --> 00:05:03,800 I am running 'os.system("clear")' 88 00:05:05,000 --> 00:05:08,500 Right. See, how it is going to work? You know 'platform.system' 89 00:05:08,500 --> 00:05:11,500 is going to give your operating system name on which 90 00:05:11,500 --> 00:05:13,000 you are running your Python script. Suppose 91 00:05:13,000 --> 00:05:15,400 if I run this script on my Windows operating system 92 00:05:15,600 --> 00:05:19,300 then this value becomes 'Windows'. If it is 'True', you know 93 00:05:19,300 --> 00:05:22,800 your python 'if' condition is allowed to execute this line, 94 00:05:23,300 --> 00:05:27,400 and you know in 'if-else', if 'if' condition is 'True', then by default 95 00:05:27,400 --> 00:05:31,300 'else' is going to skip. Same skip just assume that you are 96 00:05:31,300 --> 00:05:34,100 going to run on your Unix-like systems. Whenever if you are 97 00:05:34,200 --> 00:05:36,100 running this script on Unix-like systems, 98 00:05:36,300 --> 00:05:40,300 this value is your 'Linux', then this is 'False', right? 99 00:05:40,600 --> 00:05:44,400 If it is 'False', then 'if' doesn't allow to execute this line, 100 00:05:44,900 --> 00:05:48,300 and if this is 'False' by default without checking any condition 101 00:05:48,800 --> 00:05:51,500 'else' block will execute, right. Now, 102 00:05:51,500 --> 00:05:52,800 I am copying this code. 103 00:05:53,700 --> 00:05:58,600 First of all, let me run this on your Windows operating system. 104 00:05:58,600 --> 00:06:00,600 [no audio] 105 00:06:00,600 --> 00:06:02,000 Yes, it's prompting. 106 00:06:02,600 --> 00:06:04,500 'sys', yes. Spelling mistake guys. 107 00:06:05,000 --> 00:06:06,000 It's my bad. 108 00:06:06,000 --> 00:06:08,600 [no audio] 109 00:06:08,600 --> 00:06:10,300 's-y-s-t-e-m' 110 00:06:10,300 --> 00:06:12,600 [no audio] 111 00:06:12,600 --> 00:06:13,700 Yeah, it's correct here. 112 00:06:13,700 --> 00:06:16,300 [no audio] 113 00:06:16,300 --> 00:06:18,000 Let me run your script. 114 00:06:18,000 --> 00:06:19,700 [no audio] 115 00:06:19,700 --> 00:06:20,900 Right. Just clearing your screen. 116 00:06:21,100 --> 00:06:24,000 Same code, whatever you have here, that code 117 00:06:24,000 --> 00:06:27,200 I'm going to copy it and paste it on your Unix-like systems. 118 00:06:28,500 --> 00:06:31,200 Right. See let me open, before that 119 00:06:31,200 --> 00:06:35,200 let me nullify your previous data, and I am going to open 120 00:06:35,200 --> 00:06:38,200 your 'clear_screen' script, and I am simply copying and pasting 121 00:06:38,400 --> 00:06:42,000 your code from your Windows operating system. 122 00:06:42,000 --> 00:06:43,300 Now, I am going to run this. 123 00:06:44,500 --> 00:06:45,500 It's clearing. 124 00:06:47,000 --> 00:06:48,700 Right. See guys, be clear. 125 00:06:49,300 --> 00:06:53,400 So here I use the concept of your 'os' and 'platform' modules 126 00:06:53,400 --> 00:06:55,600 along with your 'if-else' concept. 127 00:06:56,400 --> 00:06:59,800 So now this is a platform independent script. This script 128 00:06:59,800 --> 00:07:02,700 you can use on any operating system such that it is going 129 00:07:02,700 --> 00:07:04,000 to clear your terminal. 130 00:07:04,500 --> 00:07:07,400 So one script it is going to work with all operating systems. 131 00:07:08,400 --> 00:07:13,200 Right. So this is the simple script to clear your terminal, and 132 00:07:13,200 --> 00:07:15,200 it is a platform independent script. 133 00:07:16,200 --> 00:07:17,500 So that is important guys here. 134 00:07:18,500 --> 00:07:21,600 So whenever you are writing Python script, right, and in your 135 00:07:21,600 --> 00:07:24,500 real time if you have multiple operating systems, then you 136 00:07:24,500 --> 00:07:26,900 have to write your Python script in such a way that it has to 137 00:07:26,900 --> 00:07:28,800 work with all your operating systems. 138 00:07:30,400 --> 00:07:32,100 Right. Okay. 139 00:07:32,100 --> 00:07:33,500 Thank you for watching this video. 140 00:07:33,500 --> 00:07:42,233 [no audio]