1 00:00:00,450 --> 00:00:02,969 Instructor: I wanna talk quickly about file paths, 2 00:00:02,969 --> 00:00:06,870 because for some people, this might be a new concept. 3 00:00:06,870 --> 00:00:09,240 Up until now, I wrote, 4 00:00:09,240 --> 00:00:13,020 let's say, something like sad.py or sad.text, 5 00:00:13,020 --> 00:00:14,970 and I was able to run this code. 6 00:00:14,970 --> 00:00:19,500 Because when I ran it in my terminal or command prompt, 7 00:00:19,500 --> 00:00:23,520 they were all in the same folder on my desktop. 8 00:00:23,520 --> 00:00:25,740 If I do present working directory, 9 00:00:25,740 --> 00:00:28,530 on a Mac, it shows me where I'm currently at, 10 00:00:28,530 --> 00:00:30,270 which is the desktop. 11 00:00:30,270 --> 00:00:32,310 So I was able to run it like this 12 00:00:32,310 --> 00:00:35,490 because my script file was in the same folder 13 00:00:35,490 --> 00:00:38,433 as my sad and test text. 14 00:00:39,360 --> 00:00:42,690 But what happens if this wasn't the case? 15 00:00:42,690 --> 00:00:45,900 We can actually jump back a directory 16 00:00:45,900 --> 00:00:47,970 by saying cd dot dot. 17 00:00:47,970 --> 00:00:49,560 So if we go back, 18 00:00:49,560 --> 00:00:54,510 my present working directory is now user aneagoie. 19 00:00:54,510 --> 00:00:56,550 So I'm not on the desktop anymore, 20 00:00:56,550 --> 00:00:59,297 so that if I try and run python3 21 00:01:02,370 --> 00:01:05,253 and then say script.py, 22 00:01:06,120 --> 00:01:09,780 well, can't find the file, right? 23 00:01:09,780 --> 00:01:11,590 So let's go back to Desktop 24 00:01:13,950 --> 00:01:15,960 and try to figure out this problem. 25 00:01:15,960 --> 00:01:18,790 I'm going to, for now, make a directory 26 00:01:20,400 --> 00:01:22,593 and call it, let's say, 27 00:01:23,940 --> 00:01:25,980 well, a very generic folder, 28 00:01:25,980 --> 00:01:27,513 or let's just call it app. 29 00:01:28,770 --> 00:01:31,353 So now I have an app folder. 30 00:01:32,220 --> 00:01:37,220 I can move this sad text into the app folder now. 31 00:01:37,650 --> 00:01:42,633 So if I grab the sad text and move it to the app folder, 32 00:01:43,890 --> 00:01:48,890 if I go back now and try to access sad.txt, 33 00:01:49,080 --> 00:01:53,313 so let's say python3 script.py. 34 00:01:55,410 --> 00:01:59,220 Well, if I go back, I've appended sad.txt, 35 00:01:59,220 --> 00:02:01,410 so it's actually creating it for me. 36 00:02:01,410 --> 00:02:05,790 So instead, let's actually remove sad.txt 37 00:02:05,790 --> 00:02:10,789 because we actually have one already in here, in the app. 38 00:02:11,190 --> 00:02:15,030 Instead of appending it, let's just do a read. 39 00:02:15,030 --> 00:02:19,620 If I try a read now, I get a FileNotFoundError. 40 00:02:19,620 --> 00:02:22,740 sad.txt doesn't exist because it checks the desktop, 41 00:02:22,740 --> 00:02:25,320 and on the desktop, we have nothing. 42 00:02:25,320 --> 00:02:27,390 We don't have sad.txt. 43 00:02:27,390 --> 00:02:29,580 So how can we get around this? 44 00:02:29,580 --> 00:02:31,980 Well, I simply say 45 00:02:31,980 --> 00:02:36,980 that I want to go into the directory app/sad.txt, 46 00:02:37,920 --> 00:02:39,303 so that if I run this, 47 00:02:41,880 --> 00:02:45,420 now it's reading it, but it's an unsupported operation. 48 00:02:45,420 --> 00:02:47,913 So let's just turn this into a read. 49 00:02:51,630 --> 00:02:52,920 And we don't need any of this, 50 00:02:52,920 --> 00:02:57,840 so let's just print, read here. 51 00:02:57,840 --> 00:02:58,803 If I run this, 52 00:02:59,760 --> 00:03:01,620 all right, it's working now, 53 00:03:01,620 --> 00:03:04,203 I'm able to change the file path. 54 00:03:05,460 --> 00:03:09,420 Now, if you're on a non-Unix-based system, 55 00:03:09,420 --> 00:03:11,460 that is, if you're not on a Mac or Linux 56 00:03:11,460 --> 00:03:12,600 and you're on Windows, 57 00:03:12,600 --> 00:03:14,550 most of the time, instead of this slash, 58 00:03:14,550 --> 00:03:17,730 it's a backslash, like this. 59 00:03:17,730 --> 00:03:21,450 So Windows and Unix systems have different file paths 60 00:03:21,450 --> 00:03:23,940 and file system in place. 61 00:03:23,940 --> 00:03:26,940 So this is something that you can read up on. 62 00:03:26,940 --> 00:03:28,440 Depending on your file path 63 00:03:28,440 --> 00:03:31,380 and what your present working directory is, 64 00:03:31,380 --> 00:03:34,470 you'll have a better idea of how it should look. 65 00:03:34,470 --> 00:03:35,303 On Windows, 66 00:03:35,303 --> 00:03:37,980 it's usually something like C and then backslash, 67 00:03:37,980 --> 00:03:40,890 and then you'll have a different file path. 68 00:03:40,890 --> 00:03:43,590 Now, this is called a relative path. 69 00:03:43,590 --> 00:03:45,330 So relative to where I am, 70 00:03:45,330 --> 00:03:46,980 where I'm running the script from, 71 00:03:46,980 --> 00:03:51,360 I am going to go from here into the app into sad.txt. 72 00:03:51,360 --> 00:03:56,360 I could have also copied this entire file path, like this. 73 00:03:57,870 --> 00:04:01,560 And this is called an Absolute Path. 74 00:04:01,560 --> 00:04:02,973 So if I run this, 75 00:04:04,680 --> 00:04:07,980 this still works because I'm giving an absolute path. 76 00:04:07,980 --> 00:04:10,020 It's saying, hey, on this computer, 77 00:04:10,020 --> 00:04:14,760 this exists from the root user all the way into here. 78 00:04:14,760 --> 00:04:16,470 So this is another way of doing it, 79 00:04:16,470 --> 00:04:18,390 but most of the time, you'll see relative 80 00:04:18,390 --> 00:04:20,493 because it's a lot simpler. 81 00:04:21,899 --> 00:04:26,280 Finally, you might sometimes see something like a dot slash. 82 00:04:26,280 --> 00:04:29,940 Dot slash simply means "from the current folder". 83 00:04:29,940 --> 00:04:32,970 So if I run this, it's still going to work. 84 00:04:32,970 --> 00:04:35,640 It's going to say, hey, from the current folder, 85 00:04:35,640 --> 00:04:39,750 go to app folder, then go to the text folder. 86 00:04:39,750 --> 00:04:44,040 If I do dot dot slash, like this, 87 00:04:44,040 --> 00:04:47,430 if I run this, I'll get an error. 88 00:04:47,430 --> 00:04:50,640 Dot dot means back a folder. 89 00:04:50,640 --> 00:04:54,840 So instead of our present working directory being Desktop, 90 00:04:54,840 --> 00:04:57,180 because we did dot dot, 91 00:04:57,180 --> 00:05:00,810 we'll actually go into my user account, 92 00:05:00,810 --> 00:05:04,897 and it's going to try and find aneagoie/app/sad.txt, 93 00:05:06,480 --> 00:05:07,930 which it's not going to find. 94 00:05:09,360 --> 00:05:11,190 Anyway, this is a bit of a tangent, 95 00:05:11,190 --> 00:05:12,450 but I want to let you know 96 00:05:12,450 --> 00:05:14,460 that these file paths are important, 97 00:05:14,460 --> 00:05:16,770 and it's really dependent on, 98 00:05:16,770 --> 00:05:21,600 A, what operating system you're using, like Mac or Windows, 99 00:05:21,600 --> 00:05:24,603 and also, where you're calling the file from. 100 00:05:25,890 --> 00:05:29,160 Let's continue our discussion of files in the next video. 101 00:05:29,160 --> 00:05:31,350 One quick note that I forgot to mention, 102 00:05:31,350 --> 00:05:36,210 there's a really useful built-in module in Python. 103 00:05:36,210 --> 00:05:39,090 And you can see here, it's new in version 3.4, 104 00:05:39,090 --> 00:05:40,270 called pathlib. 105 00:05:41,220 --> 00:05:44,280 It's an object-oriented filesystem path. 106 00:05:44,280 --> 00:05:46,080 Now, the main thing is, 107 00:05:46,080 --> 00:05:48,720 you can see how to use it through the documentation, 108 00:05:48,720 --> 00:05:52,170 but the key thing is that if you're building a program 109 00:05:52,170 --> 00:05:55,650 that, let's say, reads something from the file system, 110 00:05:55,650 --> 00:05:56,613 like a file, 111 00:05:57,480 --> 00:06:01,680 and this program can be used both on Windows and Mac 112 00:06:01,680 --> 00:06:03,300 and Linux systems, 113 00:06:03,300 --> 00:06:06,780 you want a way to make sure that it works on both, right? 114 00:06:06,780 --> 00:06:09,210 Because we saw how Mac and Windows 115 00:06:09,210 --> 00:06:12,450 have different file systems with different slashes. 116 00:06:12,450 --> 00:06:16,650 Well, pathlib library actually works with both. 117 00:06:16,650 --> 00:06:17,483 You can see over here, 118 00:06:17,483 --> 00:06:19,537 "If you want to manipulate Windows paths 119 00:06:19,537 --> 00:06:21,870 "on a Unix machine, or vice versa," 120 00:06:21,870 --> 00:06:23,883 this is going to work for you. 121 00:06:25,500 --> 00:06:27,870 And it's a good way for you to create software 122 00:06:27,870 --> 00:06:30,870 that is compatible with both systems 123 00:06:30,870 --> 00:06:32,670 because pathlib is going 124 00:06:32,670 --> 00:06:34,830 to take care of the file paths for you 125 00:06:34,830 --> 00:06:37,953 based on what machine is running your code. 126 00:06:39,420 --> 00:06:41,460 Now, this is something you can read on your own 127 00:06:41,460 --> 00:06:42,293 and experiment, 128 00:06:42,293 --> 00:06:45,030 but I wanted to let you know that this does exist 129 00:06:45,030 --> 00:06:47,820 and it is a useful library for you to have 130 00:06:47,820 --> 00:06:49,740 in the back of your pocket. 131 00:06:49,740 --> 00:06:50,880 That's it for now. 132 00:06:50,880 --> 00:06:52,833 I'll see you in the next one. Bye-bye.