1
00:00:00,210 --> 00:00:00,990
Hey, welcome back.

2
00:00:01,650 --> 00:00:09,600
In this video, you will learn how to use the Path Leap Library Leap, a standard Python library.

3
00:00:09,810 --> 00:00:11,970
So it comes shipped with Python.

4
00:00:12,150 --> 00:00:14,370
You don't have to install path lib.

5
00:00:14,550 --> 00:00:21,840
It is installed by default, and Leap first came in Python version 3.4.

6
00:00:22,320 --> 00:00:27,840
And it is often seen as a substitute of the OS library.

7
00:00:28,020 --> 00:00:35,670
So both OS and Purfleet are useful for file and directory operations with Python.

8
00:00:36,330 --> 00:00:44,580
But OS treats file paths as strings and path lib uses a path.

9
00:00:44,610 --> 00:00:54,660
Object type two represents paths, so file pass, text files, CSB, etc. You'll see in more detail

10
00:00:54,960 --> 00:01:00,780
what path does and how you use it to operate files and folders in Python.

11
00:01:00,930 --> 00:01:04,230
So let's begin in this or Apple here.

12
00:01:04,230 --> 00:01:13,800
I have this empty Python file and I have this folder named files and the Files folder has to text files

13
00:01:13,800 --> 00:01:23,640
inside ABC that see with some sample contents and DPF the text with some other text inside.

14
00:01:25,760 --> 00:01:31,630
Now, before to leave file paths used to be treated as.

15
00:01:32,570 --> 00:01:39,830
Let's say, if you want to read this ABC dot file to read the content of that file first, you'd have

16
00:01:39,830 --> 00:01:47,690
to think about the path of that file, which in our case, its files slash ABC, the C.

17
00:01:49,120 --> 00:01:52,760
And you would say with open p one.

18
00:01:54,660 --> 00:01:56,640
In reading mode as file.

19
00:01:57,910 --> 00:02:05,290
And see Prince file that reads right, and we get the articles correctly when we run the scripts, so

20
00:02:05,290 --> 00:02:08,229
content one is the text inside a b c.

21
00:02:09,770 --> 00:02:16,760
So you can do this, you can still do this, however, now with path lead, it is recommended to use.

22
00:02:18,130 --> 00:02:23,680
A path object type, which comes from Path Phillip input path.

23
00:02:23,800 --> 00:02:26,530
So that is a class path.

24
00:02:28,000 --> 00:02:31,750
And what you do with that class is you instantiate it.

25
00:02:34,760 --> 00:02:44,630
So pass inside brackets or parentheses, you put a string which represents a path to a file.

26
00:02:44,870 --> 00:02:48,710
Right now, if I print out P1.

27
00:02:51,620 --> 00:02:56,450
You're going to see that it looks the same as this.

28
00:02:56,510 --> 00:02:56,850
All right.

29
00:02:57,050 --> 00:02:58,520
So it looks like a string.

30
00:02:58,730 --> 00:03:01,820
However, it is not a string you should print of the type.

31
00:03:06,210 --> 00:03:07,290
Of P1.

32
00:03:08,630 --> 00:03:18,440
You're going to see that it is a posix path type, so that class produces these types of objects.

33
00:03:19,990 --> 00:03:22,330
And so was the benefits of paths.

34
00:03:22,420 --> 00:03:25,540
You could ask why not using just strings?

35
00:03:26,320 --> 00:03:28,750
Why complicating the code like that?

36
00:03:30,440 --> 00:03:37,310
Well, the benefits of the path class are not visible in this small example.

37
00:03:37,450 --> 00:03:47,030
But if your code expands so for example, you'll see now path comes in handy if we see if P1.

38
00:03:47,330 --> 00:03:56,290
So P1 is this file path exists, so exists some methods of this kind of object.

39
00:03:56,780 --> 00:04:01,580
You can check more methods by doing there after importing path.

40
00:04:01,880 --> 00:04:03,680
You can do path.

41
00:04:05,160 --> 00:04:12,510
And so you'll see that exists is here the methods I'm using now, and you have more methods as well.

42
00:04:12,780 --> 00:04:14,010
So let's use exists.

43
00:04:14,490 --> 00:04:17,970
So if the path exists, then we execute this code.

44
00:04:20,800 --> 00:04:21,070
Right.

45
00:04:22,460 --> 00:04:23,000
So run.

46
00:04:25,370 --> 00:04:32,060
And yet we go the same old because the path exists, so we got the reading correctly, so this file

47
00:04:32,060 --> 00:04:32,690
exists.

48
00:04:32,930 --> 00:04:38,750
But you could also create paths for non-existing files.

49
00:04:38,990 --> 00:04:42,350
What I mean by that, let's say you want to create a new file.

50
00:04:43,070 --> 00:04:47,810
Let's call this g h i those t t.

51
00:04:49,090 --> 00:04:54,250
So you could hear the path of the fall, even though the file doesn't exist physically on disc.

52
00:04:54,940 --> 00:05:04,240
We created it in Python and then we can write something, so change R with W to open the file for writing.

53
00:05:04,540 --> 00:05:08,620
And then here we see file that writes.

54
00:05:10,990 --> 00:05:12,280
Contents three.

55
00:05:12,970 --> 00:05:19,900
So this codes here will be executed if P1 exists, but if we could say something like.

56
00:05:21,220 --> 00:05:23,920
If not, P.1 exists, then create that file.

57
00:05:25,190 --> 00:05:30,380
And Python created, so you see GHG content three.

58
00:05:31,310 --> 00:05:36,380
So I hope that the benefits of using path is no more obvious.

59
00:05:37,160 --> 00:05:46,340
So what I'm trying to say is that you have many, many methods and properties that you can use to manipulate

60
00:05:46,340 --> 00:05:47,030
paths.

61
00:05:48,740 --> 00:05:50,690
In this case, we use exists.

62
00:05:52,820 --> 00:05:54,680
Let me show you another benefit.

63
00:05:55,070 --> 00:06:06,230
So because P1 is a specialized path object, it has capabilities, for example, to extract the name

64
00:06:06,230 --> 00:06:07,700
of the file only.

65
00:06:08,630 --> 00:06:17,660
So if you print this old prints P1, that's a name you're going to get only the file name, which could

66
00:06:17,660 --> 00:06:19,100
be useful in different scenarios.

67
00:06:19,250 --> 00:06:27,110
And you'll see how this becomes very useful when you do things like renaming files, deleting etc..

68
00:06:27,560 --> 00:06:31,250
So P1, the name gives you the file name.

69
00:06:31,250 --> 00:06:33,800
If you want to get P1, that's 10.

70
00:06:34,280 --> 00:06:35,180
Guess what?

71
00:06:35,180 --> 00:06:37,100
This returns?

72
00:06:37,370 --> 00:06:41,090
Well, it's only the file name without the extension.

73
00:06:41,360 --> 00:06:45,980
And of course, you can also get only the extension P1 that suffix.

74
00:06:47,270 --> 00:06:49,000
So that's the third altitudes.

75
00:06:49,550 --> 00:06:55,580
If you were working with a string to represent the path, you could still do this things, of course,

76
00:06:55,580 --> 00:07:01,340
but it is more work and the code is less elegant.

77
00:07:02,150 --> 00:07:12,760
So that was a path to a file, but you could also have paths to a folder, so a directory like path

78
00:07:12,770 --> 00:07:14,810
and you could say just files.

79
00:07:15,080 --> 00:07:18,890
And that points us to that files directory.

80
00:07:21,140 --> 00:07:26,600
And if you want to print out the list of files and.

81
00:07:27,790 --> 00:07:31,760
Directories inside files, you could do print pizza.

82
00:07:31,950 --> 00:07:34,000
That's Peter Deer.

83
00:07:35,070 --> 00:07:37,380
Cold, that's methods and run.

84
00:07:38,670 --> 00:07:46,620
And you'll see that's the output is a generator, so it has those file names inside, but you want to

85
00:07:46,690 --> 00:07:48,000
extract them somehow.

86
00:07:49,160 --> 00:07:53,210
And you could do the extraction, either with a full loop like four.

87
00:07:54,350 --> 00:07:58,130
Item in P2, that's easy to do.

88
00:08:00,200 --> 00:08:03,110
Print item, so that's one way.

89
00:08:04,200 --> 00:08:07,230
To extract those file paths.

90
00:08:08,180 --> 00:08:14,060
And if you're curious to print the type of those items.

91
00:08:15,440 --> 00:08:20,840
You could do that, and you'll see that these two are path objects.

92
00:08:21,410 --> 00:08:23,870
So there is consistency for good reason.

93
00:08:24,830 --> 00:08:31,730
Even the objects generated by other methods such as this one in here are also path object types.

94
00:08:32,750 --> 00:08:37,490
So P2 is a path object type, so-called posix path.

95
00:08:38,120 --> 00:08:47,720
And also the output of this was a generator containing multiple path object types, right?

96
00:08:48,410 --> 00:08:49,700
So that was one way.

97
00:08:49,880 --> 00:08:54,230
You can also convert this into a list list generator.

98
00:08:56,030 --> 00:08:56,660
Executes.

99
00:08:58,010 --> 00:09:01,580
And you get these bath objects printed out.

100
00:09:02,640 --> 00:09:06,960
Inside this list, you see the square brackets here, the causing square brackets in their.

101
00:09:07,920 --> 00:09:10,620
So that's what you need to know for now.

102
00:09:10,830 --> 00:09:18,000
I think this gave you a good idea of what path leaps of boats, but you will learn it better as we use

103
00:09:18,000 --> 00:09:23,430
it in many, many programs that we will build throughout this section with real world examples.

104
00:09:24,360 --> 00:09:25,410
So I'll talk to you later.

105
00:09:25,440 --> 00:09:25,770
Thanks.

