1 00:00:00,700 --> 00:00:02,270 Whenever we run a Python script, 2 00:00:02,270 --> 00:00:05,010 by default it runs in the directory where the script is saved. 3 00:00:05,010 --> 00:00:06,313 That's why imports work when we try 4 00:00:06,313 --> 00:00:08,099 to import another module in the same directory. 5 00:00:08,099 --> 00:00:10,808 I know there've been been languages and systems in the past though, 6 00:00:10,808 --> 00:00:13,750 where our script would've been executed in the directory where the Python 7 00:00:13,750 --> 00:00:14,580 executable lived. 8 00:00:14,580 --> 00:00:16,518 That would be really annoying. We would constantly have to change 9 00:00:16,518 --> 00:00:19,260 the directory that our script was working in. 10 00:00:19,260 --> 00:00:24,040 Almost every bit of code that we use in this course will be using the OS library. 11 00:00:24,040 --> 00:00:24,763 It's build into Python. 12 00:00:24,763 --> 00:00:29,429 If you find yourself unable to run a file or follow along with a bit of code, 13 00:00:29,429 --> 00:00:33,209 make sure you've imported the library by using import os. 14 00:00:33,209 --> 00:00:38,898 Now we can see what directory we're in, also know as the current working directory 15 00:00:38,898 --> 00:00:44,035 by using the getcwd function, os.getcwd or current working directory. 16 00:00:45,577 --> 00:00:48,437 And I can see that I'm here, in my users folder, 17 00:00:48,437 --> 00:00:52,587 inside of a projects directory, inside of a file_systems directory. 18 00:00:52,587 --> 00:00:55,307 This will of course work the same, no matter what directory we're in. 19 00:00:55,307 --> 00:00:58,897 For example, let's make a directory here named backups. 20 00:00:58,897 --> 00:01:05,940 So I'm gonna open up a tab, and I'm gonna make a directory here named backups. 21 00:01:05,940 --> 00:01:08,370 And I'm gonna change my directory into backups, and 22 00:01:08,370 --> 00:01:09,940 then I'm gonna use Python there. 23 00:01:11,062 --> 00:01:14,884 And I'm gonna import os, and I'm gonna do os.getcwd and 24 00:01:14,884 --> 00:01:19,060 I see now that I'm inside of the backups directory. 25 00:01:19,060 --> 00:01:20,550 So now we can see where we are. 26 00:01:20,550 --> 00:01:22,720 What if we find out that we're somewhere we don't be though? 27 00:01:22,720 --> 00:01:25,100 Or we're not yet where we want to do our work? 28 00:01:25,100 --> 00:01:28,680 We can use the chdir, or change directory function, 29 00:01:28,680 --> 00:01:31,350 to change the directory that Python's currently working in. 30 00:01:31,350 --> 00:01:33,710 So I can do os.chdir('..'). 31 00:01:33,710 --> 00:01:38,830 And then, os.get current working directory. 32 00:01:38,830 --> 00:01:43,304 And now I see that I'm in the file_systems directory and not the backups directory. 33 00:01:43,304 --> 00:01:46,310 But wait, what's this dot dot thing? 34 00:01:46,310 --> 00:01:49,500 Let's take a second to talk about relative and absolute paths. 35 00:01:49,500 --> 00:01:53,260 We have two types of paths on computers, absolute and relative. 36 00:01:53,260 --> 00:01:56,700 An absolute path is the entire full path all the way from the root 37 00:01:56,700 --> 00:01:59,310 to whatever location you want. 38 00:01:59,310 --> 00:02:05,250 If my next course was in C:\Users\Kenneth\my_next_course.py, 39 00:02:05,250 --> 00:02:07,330 this is an absolute path. 40 00:02:07,330 --> 00:02:11,080 A relative path though is a path to one location from another location. 41 00:02:11,080 --> 00:02:15,920 If my current working directory is /home/workspace/backups/ I could point to 42 00:02:15,920 --> 00:02:21,380 the directory above /home/workspace as the /../ directory. 43 00:02:21,380 --> 00:02:23,970 Double dots means move up one directory. 44 00:02:23,970 --> 00:02:26,450 A single dot means the current directory. 45 00:02:26,450 --> 00:02:28,580 You can use as many of these as you want too, so 46 00:02:28,580 --> 00:02:32,230 you can chain dot dots to move up several directories. 47 00:02:32,230 --> 00:02:35,550 Relative paths can also move down the tree and won't use single or 48 00:02:35,550 --> 00:02:37,000 double dots at all. 49 00:02:37,000 --> 00:02:41,040 Usually these start with just the next directories name and no leading slash. 50 00:02:41,040 --> 00:02:43,580 If I was in the /home/workspaces directory, 51 00:02:43,580 --> 00:02:46,915 I could point to the backups directory by just saying, backups/. 52 00:02:48,230 --> 00:02:52,150 So a relative path says how to get there from here, while an absolute path covers 53 00:02:52,150 --> 00:02:55,000 getting to there from the absolute beginning of your file system. 54 00:02:55,000 --> 00:02:58,510 Python has a handy function to tell us if a path is relative or absolute. 55 00:02:58,510 --> 00:03:00,331 It lives in the path part of OS. 56 00:03:00,331 --> 00:03:05,080 So we can do os.path.isabs and we can pass in a path. 57 00:03:08,190 --> 00:03:10,120 And we get back that that is true. 58 00:03:10,120 --> 00:03:13,070 That is a absolute path. 59 00:03:13,070 --> 00:03:20,010 If we were to give a relative path though, like /workspaces, we would get back false. 60 00:03:20,010 --> 00:03:21,930 Because that's not an absolute path. 61 00:03:21,930 --> 00:03:25,359 Now, if you're on Windows and I can't show you this sadly because I'm on a Mac at 62 00:03:25,359 --> 00:03:28,300 this point, you would have to do your path a little bit differently. 63 00:03:28,300 --> 00:03:34,180 You'd have to do something like this to do the C:\\. 64 00:03:34,180 --> 00:03:36,440 Now I have to use the double backslashes there for 65 00:03:36,440 --> 00:03:39,540 Windows style paths, and it's because the single backslash 66 00:03:39,540 --> 00:03:42,320 is usually used to show that the next character is special. 67 00:03:42,320 --> 00:03:43,670 It's called an escape. 68 00:03:43,670 --> 00:03:46,340 If we don't use the double backslash, Python thinks we're trying to use some 69 00:03:46,340 --> 00:03:50,850 special character here, where the slash capital U is. 70 00:03:50,850 --> 00:03:53,090 And that's actually not a special character. 71 00:03:54,490 --> 00:03:58,340 And notice, too, here that I can make up paths. 72 00:03:58,340 --> 00:04:02,238 Python's just checking to see if it looks like an absolute path, not whether or 73 00:04:02,238 --> 00:04:04,054 not it actually is an absolute path. 74 00:04:04,054 --> 00:04:08,005 If os.path.isabs gives us back false, we know that it's a relative path. 75 00:04:08,005 --> 00:04:10,660 If it gives us back true, we know it's an absolute path. 76 00:04:10,660 --> 00:04:12,649 There's a lot more in the os.path. 77 00:04:12,649 --> 00:04:14,520 We're gonna spend a while in there during this course. 78 00:04:16,483 --> 00:04:19,530 If you haven't already, be sure to check out the docs for the OS module. 79 00:04:19,530 --> 00:04:21,220 There is a ton of stuff in there, and 80 00:04:21,220 --> 00:04:23,580 there's no way I can cover it all in a single course. 81 00:04:23,580 --> 00:04:25,140 There's a link to the docs in the teacher's notes. 82 00:04:26,190 --> 00:04:29,950 Now that you're comfortable with what a path is let's see how Python can construct 83 00:04:29,950 --> 00:04:31,210 paths for us. 84 00:04:31,210 --> 00:04:33,289 This will help us avoid having to use that double backslash or 85 00:04:33,289 --> 00:04:35,104 pay attention to which direction our slashes face.