1
00:00:00,390 --> 00:00:07,980
Previously you learned that you could get a list of Python built in modules using sys.building_module-names.

2
00:00:07,980 --> 00:00:17,190
As an example we use the time module which would freeze the script execution for a particular amount

3
00:00:17,190 --> 00:00:18,480
of seconds.

4
00:00:18,480 --> 00:00:20,310
Now let me show you something.

5
00:00:20,610 --> 00:00:29,650
I'll execute a script, onion will be printed out because it's there in the vegetables.txt file.

6
00:00:29,650 --> 00:00:33,340
However if I now delete this file,

7
00:00:37,570 --> 00:00:44,030
you'll see that the next time the script checks for the files vegetables.txt path,

8
00:00:44,140 --> 00:00:51,810
so that's a file path it's going to generate an error: "File not found error No such file or directory."

9
00:00:53,260 --> 00:00:58,260
So imagine you had this program running on a server 24/7.

10
00:00:58,390 --> 00:01:03,910
And for some reason the software that was generating these files at some point it doesn't generate the

11
00:01:03,910 --> 00:01:04,240
file.

12
00:01:04,450 --> 00:01:12,220
So your script execution will end and maybe in a minute your software will start to generate the

13
00:01:12,220 --> 00:01:19,080
files again, but your python script will not resume because it's got an error here.

14
00:01:19,090 --> 00:01:26,050
So what we want to do? We want to keep executing the python script even when the file is not in the directory

15
00:01:26,110 --> 00:01:26,650
there.

16
00:01:26,680 --> 00:01:35,950
To do that we want to make use of the OS module.
Import os and you're going to notice that os is

17
00:01:35,950 --> 00:01:44,950
not among the built in Python modules.
Indeed OS is a module written in Python.

18
00:01:45,060 --> 00:01:51,060
All these modules are built in which means they are written in C language inside the Python software

19
00:01:51,060 --> 00:01:57,410
implementation, OS is not one of them.
To find out where OS is staying

20
00:01:57,420 --> 00:01:59,480
you can use sys.prefix.

21
00:01:59,500 --> 00:02:08,340
You're going to get a directory path there which could be different depending on what

22
00:02:08,370 --> 00:02:10,260
operating system you are using.

23
00:02:10,290 --> 00:02:12,840
So please navigate to that directory.

24
00:02:12,840 --> 00:02:20,010
I prefer to go there directly by copying it without the quotes, so copy everything without the quotes

25
00:02:21,000 --> 00:02:22,050
like that.

26
00:02:22,050 --> 00:02:30,450
Go to your command line terminal and if you are on Windows you start and paste the directory there and

27
00:02:30,450 --> 00:02:31,610
then press enter.

28
00:02:31,740 --> 00:02:36,750
If you are on Mac or Linux use open instead of start and paste the directory.

29
00:02:36,750 --> 00:02:43,620
Again press enter that will open your windows explorer or finder whatever you have to open folders.

30
00:02:44,430 --> 00:02:51,090
So that the 3.7 folder that you see in here and then go to Lib.

31
00:02:51,500 --> 00:02:59,150
Double click then find the folder Python3.7 or whatever version you have.

32
00:02:59,150 --> 00:03:01,970
So Python three point something.

33
00:03:01,970 --> 00:03:07,810
Double click it and here you'll see a number of Python files dot py.

34
00:03:07,820 --> 00:03:09,800
These are all Python files.

35
00:03:09,800 --> 00:03:16,160
These are all standard Python modules and OS is among them.

36
00:03:16,160 --> 00:03:19,310
Go to O here.

37
00:03:19,430 --> 00:03:22,790
Double Click It or open it with wherever you use

38
00:03:22,790 --> 00:03:27,300
as a text editor and you'll see that this is actually Python code.

39
00:03:27,950 --> 00:03:34,910
Make sure to close it before changing it so don't change don't modify Python standard modules.

40
00:03:35,300 --> 00:03:45,190
So I'm going to close it, and you can use deer actually to see what you have available for that module.

41
00:03:45,640 --> 00:03:52,090
So among them for example I'm going to use the path object.

42
00:03:52,270 --> 00:04:02,020
So with os.path, and path has a method called exists.
The exists method gets an argument which

43
00:04:02,170 --> 00:04:09,520
should be a string and it's supposed to be the path of the file you want to check if it exists in the

44
00:04:09,520 --> 00:04:10,710
current directory.

45
00:04:13,450 --> 00:04:21,580
So let's write the directory path to the file,
and I get false because the file was not in the current

46
00:04:21,580 --> 00:04:22,350
directory.

47
00:04:22,390 --> 00:04:28,150
If I go there and create that file inside the files folder

48
00:04:32,170 --> 00:04:39,760
so now I have a files/vegetables.txt file path.
If I execute that again I'm going to get

49
00:04:39,760 --> 00:04:47,740
true and we can make use of that fact in our Python program.

50
00:04:47,740 --> 00:04:55,990
So what we want to do is before opening the file in read mode we want to check if the file exists

51
00:04:56,290 --> 00:05:03,760
because if we don't do that and the file is not there this line here is going to generate that file

52
00:05:03,790 --> 00:05:04,740
not found error.

53
00:05:05,230 --> 00:05:17,180
So before doing that we want to check if os.path.exists files/vegetables.txt

54
00:05:17,300 --> 00:05:26,840
and since this is a conditional we want to use a colon there and after that wherever you want to

55
00:05:26,840 --> 00:05:29,320
do it has to be indented.

56
00:05:29,660 --> 00:05:41,450
So with it's going to be under if. This is also part of the with block so indent that as well.

57
00:05:41,560 --> 00:05:48,410
And then else goes inside the while loop again.
What you want to do under else?

58
00:05:48,520 --> 00:05:58,650
Well maybe we want to print out "File does not exist" and then be careful here.

59
00:05:58,690 --> 00:06:02,440
Time sleep has to be outside of the if else block.

60
00:06:02,500 --> 00:06:11,350
Because you want to execute this independently of the conditional block which means if the file

61
00:06:11,350 --> 00:06:12,850
exists,

62
00:06:12,850 --> 00:06:16,360
this is going to be executed. if the file does not exist

63
00:06:16,360 --> 00:06:24,100
this is going to be executed, and after either of these is executed, always we are going to execute

64
00:06:24,100 --> 00:06:25,540
time.sleep.

65
00:06:25,540 --> 00:06:30,220
So let me save the script and execute it.

66
00:06:30,220 --> 00:06:33,050
And we got an error, os it's not defined.

67
00:06:33,050 --> 00:06:38,750
I am glad that this showed up because modules have to be imported.

68
00:06:39,820 --> 00:06:46,420
Otherwise you're going to get that name error because os before being used in here it was not

69
00:06:46,420 --> 00:06:53,790
imported, and it's not a built in function or methods such as print which you don't need to import.

70
00:06:53,800 --> 00:07:01,390
So its name is not available immediately in the namespace.
Save the script again, execute, and we get an

71
00:07:01,390 --> 00:07:05,650
empty string because vegetables is currently empty.

72
00:07:05,650 --> 00:07:13,560
Now if I go and delete some vegetables like that, wait a while

73
00:07:17,570 --> 00:07:19,870
and we get this printed out.

74
00:07:20,360 --> 00:07:21,770
If you create the file again.

75
00:07:25,340 --> 00:07:33,830
Write something there, save the file, don't forget, pretending that the software it's generating this file.

76
00:07:36,110 --> 00:07:39,210
So you get garlic there.

77
00:07:39,230 --> 00:07:47,510
Now I can interrupt that script execution with control C and those are both standard Python modules

78
00:07:47,840 --> 00:07:49,390
and they are exactly the same.

79
00:07:49,400 --> 00:07:54,780
So you can use dir with them.

80
00:07:55,710 --> 00:07:59,030
So all the Python rules apply to this modules.

81
00:07:59,040 --> 00:08:05,280
The only difference is that time is written in C language, and it's built in inside the python software

82
00:08:05,580 --> 00:08:13,320
and OS comes as a .py script and it came here when you installed Python so it was basically copied

83
00:08:13,740 --> 00:08:20,580
in one of your computer directories and you can import those, 
all those modules standard modules

84
00:08:21,000 --> 00:08:22,230
in Python.

85
00:08:22,230 --> 00:08:23,580
Now, of course

86
00:08:23,610 --> 00:08:26,940
not everything comes with your Python installation.

87
00:08:27,030 --> 00:08:29,520
Some things you have to install.

88
00:08:29,610 --> 00:08:31,350
So I'll show you that in the next video.

