1
00:00:00,000 --> 00:00:04,000
Now, the next task that I'm actually going to do over here is that let's go ahead and create a new

2
00:00:04,000 --> 00:00:05,000
directory okay.

3
00:00:05,000 --> 00:00:10,000
So create a new directory I'll be doing this.

4
00:00:11,000 --> 00:00:13,000
Um, let's say I will again you need to import OS.

5
00:00:13,000 --> 00:00:17,000
So let's say my new directory is something like this okay.

6
00:00:17,000 --> 00:00:24,000
Let's say this is my new directory called as package okay I want to create a package folder, uh, over

7
00:00:24,000 --> 00:00:29,000
here in the same working location that I'm actually writing a package folder over here in the file handling

8
00:00:29,000 --> 00:00:30,000
folder itself inside this.

9
00:00:30,000 --> 00:00:36,000
So all I have to do is that I'll just go ahead and write OS dot make dir, and I'm just going to give

10
00:00:36,000 --> 00:00:38,000
my new directory information.

11
00:00:38,000 --> 00:00:40,000
And let me just go ahead and print this.

12
00:00:40,000 --> 00:00:44,000
I'll use an F string and say that directory.

13
00:00:45,000 --> 00:00:52,000
Uh and here I'm going to specifically use this new directory name is created okay.

14
00:00:53,000 --> 00:00:54,000
So this will be my message.

15
00:00:54,000 --> 00:00:57,000
So here you can see directory package created okay.

16
00:00:57,000 --> 00:01:01,000
And here you can probably see I will be able to get this package folder okay.

17
00:01:01,000 --> 00:01:05,000
And this is how you can create a new directory with the help of OS.

18
00:01:05,000 --> 00:01:06,000
Okay.

19
00:01:06,000 --> 00:01:12,000
Let's say that I want to list all the files from a directory itself.

20
00:01:12,000 --> 00:01:12,000
Right.

21
00:01:12,000 --> 00:01:17,000
So next task will be that listing files and directories.

22
00:01:17,000 --> 00:01:22,000
I want to list all the files, and I want to list all the folders that I have inside a folder itself

23
00:01:22,000 --> 00:01:23,000
where I'm actually working.

24
00:01:23,000 --> 00:01:31,000
So I will just go ahead and write items is equal to OS dot list dir.

25
00:01:31,000 --> 00:01:35,000
And here I'm just going to use this dot okay.

26
00:01:35,000 --> 00:01:40,000
Dot basically means we are specifically looking at the current working location, whichever folder it

27
00:01:40,000 --> 00:01:44,000
is inside, and it will just list and it'll give you the entire information.

28
00:01:44,000 --> 00:01:48,000
Okay, so, uh, let us go ahead and display this entire item.

29
00:01:48,000 --> 00:01:51,000
So if I go ahead and print items over here.

30
00:01:51,000 --> 00:01:57,000
So here you'll be able to see I have this particular file, this particular file destination txt example

31
00:01:57,000 --> 00:01:58,000
dot bin package.

32
00:01:58,000 --> 00:02:03,000
So all the folders inside this particular folder it is being able to give me okay.

33
00:02:03,000 --> 00:02:04,000
Pretty much amazing.

34
00:02:04,000 --> 00:02:08,000
Uh and definitely we use this kind of file a lot because.

35
00:02:09,000 --> 00:02:15,000
I may have a scenario that in my project, I need to probably read all the files and folders, and I

36
00:02:15,000 --> 00:02:18,000
need to probably do some kind of operations, and I can actually do that.

37
00:02:18,000 --> 00:02:19,000
Okay.

38
00:02:19,000 --> 00:02:26,000
Now talking about one more very important concept that is nothing but joining paths, okay.

39
00:02:26,000 --> 00:02:32,000
And this is the most important feature of using this OS package.

40
00:02:32,000 --> 00:02:35,000
You know, because this path will be different.

41
00:02:35,000 --> 00:02:38,000
Different when terms of coding and windows separately coding.

42
00:02:38,000 --> 00:02:41,000
You can just hard code it right in Linux or in Mac.

43
00:02:41,000 --> 00:02:46,000
So uh, by using OS dot path, uh, we can specifically use this.

44
00:02:46,000 --> 00:02:46,000
Okay.

45
00:02:46,000 --> 00:02:52,000
Let's say that I want to probably create I have a directory name which is called as folder okay.

46
00:02:52,000 --> 00:02:57,000
And then let's say I have my file underscore name okay.

47
00:02:57,000 --> 00:02:59,000
And this will be my file dot txt.

48
00:03:00,000 --> 00:03:03,000
Now how do I create this particular path.

49
00:03:03,000 --> 00:03:07,000
That basically means inside my folder there is a file dot txt right.

50
00:03:07,000 --> 00:03:09,000
If I want to probably get the full path or relative path.

51
00:03:09,000 --> 00:03:18,000
Also, I can say I can just write os dot path okay dot join, then I can actually use this directory

52
00:03:18,000 --> 00:03:18,000
name.

53
00:03:19,000 --> 00:03:19,000
Okay.

54
00:03:19,000 --> 00:03:21,000
Oops, just a second.

55
00:03:21,000 --> 00:03:25,000
I can directly use this directory underscore name comma.

56
00:03:25,000 --> 00:03:28,000
I will go ahead and use file underscore name okay.

57
00:03:28,000 --> 00:03:30,000
So this basically becomes my full path.

58
00:03:30,000 --> 00:03:38,000
So now if I go ahead and print my full path you will be able to see hey this is my path that I'm actually

59
00:03:38,000 --> 00:03:39,000
getting right.

60
00:03:39,000 --> 00:03:43,000
Let's say I want to completely get it along with my folder location.

61
00:03:43,000 --> 00:03:46,000
Then I can copy this entire thing again.

62
00:03:46,000 --> 00:03:52,000
Okay, I can paste it over here and here I can also do OS dot get cw d.

63
00:03:54,000 --> 00:03:56,000
Current working directory along with my directory name.

64
00:03:57,000 --> 00:04:01,000
Okay, so if I go ahead and write this now here you can see this is my entire path.

65
00:04:01,000 --> 00:04:02,000
Right.

66
00:04:02,000 --> 00:04:07,000
I can also do this if I want to just have the relative path I can just use this two variables.

67
00:04:07,000 --> 00:04:08,000
Okay.

68
00:04:08,000 --> 00:04:11,000
And this is how, uh, things are amazing over here.

69
00:04:11,000 --> 00:04:16,000
And you'll be able to see it now in our previous, uh, tutorials we have seen, right, whenever we

70
00:04:16,000 --> 00:04:19,000
are trying to open a file, first of all, we need to create that file, right?

71
00:04:19,000 --> 00:04:25,000
But here, uh, we will try to manipulate, you know, or let me just show you one example.

72
00:04:25,000 --> 00:04:26,000
Okay.

73
00:04:26,000 --> 00:04:30,000
So here, uh, what I'm actually going to do, I'm going to create an example file.

74
00:04:30,000 --> 00:04:41,000
So let's say this is my example one dot txt I will say hey if os dot path dot exist okay?

75
00:04:41,000 --> 00:04:48,000
If this particular path exist or this particular folder exist, then I will just go ahead and write

76
00:04:48,000 --> 00:04:52,000
print f okay, print f.

77
00:04:52,000 --> 00:04:55,000
Here you can actually see the path.

78
00:04:57,000 --> 00:05:02,000
The path this specific path exists.

79
00:05:02,000 --> 00:05:02,000
Okay.

80
00:05:03,000 --> 00:05:05,000
That basically means that folder also exists.

81
00:05:05,000 --> 00:05:13,000
Else I will just go ahead and print f the path.

82
00:05:17,000 --> 00:05:19,000
Path does not exist.

83
00:05:24,000 --> 00:05:24,000
Okay.

84
00:05:25,000 --> 00:05:29,000
So here if I go ahead and execute it here you can see that the path example one dot txt file does not

85
00:05:29,000 --> 00:05:31,000
exist because I don't have that specific file.

86
00:05:32,000 --> 00:05:35,000
Now let's say if this file is not there what I can do I can.

87
00:05:35,000 --> 00:05:40,000
If it is not there or if this file does not exist, I will go ahead and create that particular file

88
00:05:40,000 --> 00:05:44,000
with again you or with open command or with any kind of command.

89
00:05:44,000 --> 00:05:45,000
I can go ahead and do it right.

90
00:05:46,000 --> 00:05:47,000
So this is very much important.

91
00:05:47,000 --> 00:05:53,000
And this kind of condition needs to be used in our coding environment whenever we are making some assumptions

92
00:05:53,000 --> 00:05:55,000
of a specific path itself.

93
00:05:55,000 --> 00:05:55,000
Okay.

94
00:05:57,000 --> 00:05:58,000
Now this is fine.

95
00:05:58,000 --> 00:06:01,000
There is also one more way to check whether the file is present or not.

96
00:06:01,000 --> 00:06:01,000
Okay.

97
00:06:02,000 --> 00:06:03,000
Um, so here I will show you.

98
00:06:06,000 --> 00:06:10,000
So I will just go ahead and write this comment checking if the path is a file or directory.

99
00:06:10,000 --> 00:06:13,000
So there is one more method over here.

100
00:06:13,000 --> 00:06:17,000
So here you can see import OS I'm using path is equal to example dot txt.

101
00:06:17,000 --> 00:06:21,000
Now this file right OS dot path dot is file of path.

102
00:06:21,000 --> 00:06:24,000
So this file it is going to check whether this path is a file.

103
00:06:24,000 --> 00:06:28,000
If it is a file then it is going to say it is a file if this path is a directory.

104
00:06:28,000 --> 00:06:31,000
So for that I have is directory function.

105
00:06:31,000 --> 00:06:36,000
Then it will go and say that hey this path is a directory as this path is neither a file or directory.

106
00:06:36,000 --> 00:06:39,000
So if I go ahead and execute it I will go ahead and see this.

107
00:06:39,000 --> 00:06:39,000
Right.

108
00:06:39,000 --> 00:06:43,000
So here it shows the path example dot txt is the file okay.

109
00:06:44,000 --> 00:06:47,000
So uh this is how things are basically working over here.

110
00:06:47,000 --> 00:06:48,000
And you will be able to see that.

111
00:06:48,000 --> 00:06:53,000
Hey yeah this particular file is a file because example dot txt is a file.

112
00:06:53,000 --> 00:06:53,000
Okay.

113
00:06:54,000 --> 00:06:56,000
Uh as I already told you okay.

114
00:06:56,000 --> 00:06:57,000
Relative path is something different.

115
00:06:57,000 --> 00:07:01,000
Relative path basically means this file inside this particular folder file dot txt.

116
00:07:01,000 --> 00:07:03,000
If I'm seeing this becomes a relative path okay.

117
00:07:03,000 --> 00:07:03,000
Okay.

118
00:07:04,000 --> 00:07:07,000
Uh, and the absolute path is the complete path okay.

119
00:07:07,000 --> 00:07:11,000
Let's say for any folder location I want to get the absolute path okay.

120
00:07:11,000 --> 00:07:13,000
Getting the absolute path.

121
00:07:13,000 --> 00:07:16,000
Getting the absolute path.

122
00:07:16,000 --> 00:07:18,000
Because I may be requiring some or the other thing over here.

123
00:07:19,000 --> 00:07:19,000
Okay.

124
00:07:19,000 --> 00:07:20,000
So here what I will do.

125
00:07:20,000 --> 00:07:23,000
Let's say that this is my relative path.

126
00:07:23,000 --> 00:07:27,000
So this file is basically present inside this folder which is called as example dot txt.

127
00:07:27,000 --> 00:07:31,000
Now I will go ahead and create my absolute path.

128
00:07:32,000 --> 00:07:35,000
Absolute path okay.

129
00:07:35,000 --> 00:07:40,000
And for this I will go ahead and write OS dot path dot apps path.

130
00:07:40,000 --> 00:07:43,000
So apps path is one uh method over here.

131
00:07:43,000 --> 00:07:46,000
And then we can use this relative path okay.

132
00:07:46,000 --> 00:07:52,000
Now if I go ahead and just print my absolute path here, you'll be able to see the complete path.

133
00:07:52,000 --> 00:07:54,000
I'll be able to get it right again.

134
00:07:54,000 --> 00:07:59,000
Uh, if you do not want to go ahead and do in this way, you can specifically go ahead and join all

135
00:07:59,000 --> 00:08:01,000
this particular things along with the directory name.

136
00:08:01,000 --> 00:08:05,000
But I feel, uh, this is again, better when compared to that one.

137
00:08:05,000 --> 00:08:10,000
So I hope, guys, you were able to understand all this file path operations, specifically with the

138
00:08:10,000 --> 00:08:15,000
help of, uh, OS uh, again, there will be multiple examples which will be seeing.

139
00:08:15,000 --> 00:08:20,000
But most of the common operations that we usually do in the code, uh, is probably discussed over here.

140
00:08:20,000 --> 00:08:25,000
This will be the most useful operation or most useful library when you are working in an end to end

141
00:08:25,000 --> 00:08:29,000
projects, working with different different files, working with different different directories over

142
00:08:29,000 --> 00:08:30,000
there.

143
00:08:30,000 --> 00:08:32,000
Uh, so there we are going to extensively use it.

144
00:08:32,000 --> 00:08:33,000
Right.

145
00:08:33,000 --> 00:08:38,000
So please make sure that you practice this more see different different examples with respect to this.

146
00:08:38,000 --> 00:08:40,000
And yeah, uh, this was it from my side.

147
00:08:40,000 --> 00:08:42,000
I will see you all in the next video.

148
00:08:42,000 --> 00:08:43,000
Thank you.

149
00:08:43,000 --> 00:08:43,000
Take care.

