Changing Directories4:36 with Kenneth Love
Learn how to change directories from our Python scripts and we'll start looking at the `os` module.
Whenever we run a Python script, 0:00 by default it runs in the directory where the script is saved. 0:02 That's why imports work when we try 0:05 to import another module in the same directory. 0:06 I know there've been been languages and systems in the past though, 0:08 where our script would've been executed in the directory where the Python 0:10 executable lived. 0:13 That would be really annoying. We would constantly have to change 0:14 the directory that our script was working in. 0:16 Almost every bit of code that we use in this course will be using the OS library. 0:19 It's build into Python. 0:24 If you find yourself unable to run a file or follow along with a bit of code, 0:24 make sure you've imported the library by using import os. 0:29 Now we can see what directory we're in, also know as the current working directory 0:33 by using the getcwd function, os.getcwd or current working directory. 0:38 And I can see that I'm here, in my users folder, 0:45 inside of a projects directory, inside of a file_systems directory. 0:48 This will of course work the same, no matter what directory we're in. 0:52 For example, let's make a directory here named backups. 0:55 So I'm gonna open up a tab, and I'm gonna make a directory here named backups. 0:58 And I'm gonna change my directory into backups, and 1:05 then I'm gonna use Python there. 1:08 And I'm gonna import os, and I'm gonna do os.getcwd and 1:11 I see now that I'm inside of the backups directory. 1:14 So now we can see where we are. 1:19 What if we find out that we're somewhere we don't be though? 1:20 Or we're not yet where we want to do our work? 1:22 We can use the chdir, or change directory function, 1:25 to change the directory that Python's currently working in. 1:28 So I can do os.chdir('..'). 1:31 And then, os.get current working directory. 1:33 And now I see that I'm in the file_systems directory and not the backups directory. 1:38 But wait, what's this dot dot thing? 1:43 Let's take a second to talk about relative and absolute paths. 1:46 We have two types of paths on computers, absolute and relative. 1:49 An absolute path is the entire full path all the way from the root 1:53 to whatever location you want. 1:56 If my next course was in C:\Users\Kenneth\my_next_course.py, 1:59 this is an absolute path. 2:05 A relative path though is a path to one location from another location. 2:07 If my current working directory is /home/workspace/backups/ I could point to 2:11 the directory above /home/workspace as the /../ directory. 2:15 Double dots means move up one directory. 2:21 A single dot means the current directory. 2:23 You can use as many of these as you want too, so 2:26 you can chain dot dots to move up several directories. 2:28 Relative paths can also move down the tree and won't use single or 2:32 double dots at all. 2:35 Usually these start with just the next directories name and no leading slash. 2:37 If I was in the /home/workspaces directory, 2:41 I could point to the backups directory by just saying, backups/. 2:43 So a relative path says how to get there from here, while an absolute path covers 2:48 getting to there from the absolute beginning of your file system. 2:52 Python has a handy function to tell us if a path is relative or absolute. 2:55 It lives in the path part of OS. 2:58 So we can do os.path.isabs and we can pass in a path. 3:00 And we get back that that is true. 3:08 That is a absolute path. 3:10 If we were to give a relative path though, like /workspaces, we would get back false. 3:13 Because that's not an absolute path. 3:20 Now, if you're on Windows and I can't show you this sadly because I'm on a Mac at 3:21 this point, you would have to do your path a little bit differently. 3:25 You'd have to do something like this to do the C:\\. 3:28 Now I have to use the double backslashes there for 3:34 Windows style paths, and it's because the single backslash 3:36 is usually used to show that the next character is special. 3:39 It's called an escape. 3:42 If we don't use the double backslash, Python thinks we're trying to use some 3:43 special character here, where the slash capital U is. 3:46 And that's actually not a special character. 3:50 And notice, too, here that I can make up paths. 3:54 Python's just checking to see if it looks like an absolute path, not whether or 3:58 not it actually is an absolute path. 4:02 If os.path.isabs gives us back false, we know that it's a relative path. 4:04 If it gives us back true, we know it's an absolute path. 4:08 There's a lot more in the os.path. 4:10 We're gonna spend a while in there during this course. 4:12 If you haven't already, be sure to check out the docs for the OS module. 4:16 There is a ton of stuff in there, and 4:19 there's no way I can cover it all in a single course. 4:21 There's a link to the docs in the teacher's notes. 4:23 Now that you're comfortable with what a path is let's see how Python can construct 4:26 paths for us. 4:29 This will help us avoid having to use that double backslash or 4:31 pay attention to which direction our slashes face. 4:33
You need to sign up for Treehouse in order to download course files.Sign up