1 00:00:07,940 --> 00:00:12,450 So far, we've pretended that all files live in a single folder or 2 00:00:12,450 --> 00:00:17,760 directory and it's the directory that your Python program is connected to. 3 00:00:17,760 --> 00:00:23,835 So, your Python program doesn't need to specify a location for the file when opening it. 4 00:00:23,835 --> 00:00:28,695 With our simulated files in the Runestone environment that worked fine, 5 00:00:28,695 --> 00:00:33,505 but if you're ever running Python on a local machine it probably won't be enough for you. 6 00:00:33,505 --> 00:00:37,340 So, let's see how you can have files organized 7 00:00:37,340 --> 00:00:41,270 into folders and directories and still find them in your Python program. 8 00:00:41,270 --> 00:00:44,600 The most people do organize their files into folders or 9 00:00:44,600 --> 00:00:46,550 directories because otherwise you just have a hundreds 10 00:00:46,550 --> 00:00:49,180 of files in one directory and you can't find them. 11 00:00:49,180 --> 00:00:55,460 So, for example, here's a diagram showing a hierarchy of directories and files. 12 00:00:55,460 --> 00:01:01,460 There's a top-level directory called myFiles and inside 13 00:01:01,460 --> 00:01:08,640 that directory we have two other sub-directories, otherFiles, and allProjects. 14 00:01:08,640 --> 00:01:16,500 AllProjects itself has two more sub-directories called myData and myProject. 15 00:01:16,500 --> 00:01:22,515 Inside of myData, we've got a couple of files, one's called data2.txt. 16 00:01:22,515 --> 00:01:29,325 In myProject, we've got data1.txt and myPythonProgram.py. 17 00:01:29,325 --> 00:01:36,115 By convention, we put Python programs into files whose name ends in.py. 18 00:01:36,115 --> 00:01:39,315 So a Python program, when we run it, 19 00:01:39,315 --> 00:01:42,925 is automatically connected to the directory where it's invoked from. 20 00:01:42,925 --> 00:01:45,500 We are invoking Python directly in 21 00:01:45,500 --> 00:01:47,840 the Runestone book so we can't really see how that works, 22 00:01:47,840 --> 00:01:50,870 but I can give you a way to think about it to use if and when you 23 00:01:50,870 --> 00:01:54,805 do install Python on your own computer to run it locally. 24 00:01:54,805 --> 00:01:56,980 In the open function, 25 00:01:56,980 --> 00:02:00,780 you can pass just to filename as we've been doing 26 00:02:13,450 --> 00:02:19,310 like that, open data1.txt for reading, 27 00:02:19,310 --> 00:02:22,750 but we also can specify 28 00:02:22,750 --> 00:02:27,770 a complete path that says where to find the file as well as the file's name. 29 00:02:27,770 --> 00:02:32,800 So normally, we would use a relative path which specifies how to 30 00:02:32,800 --> 00:02:37,735 get to another directory from the directory that you're currently connected to. 31 00:02:37,735 --> 00:02:42,685 So suppose that we're running this myPythonProgram.py, 32 00:02:42,685 --> 00:02:46,870 and we're running it from the directory myProject, 33 00:02:46,870 --> 00:02:52,550 and we want to open data2.txt. 34 00:02:52,680 --> 00:03:02,000 We can't just say open data2, 35 00:03:03,600 --> 00:03:06,460 we have to tell it how to find it. 36 00:03:06,460 --> 00:03:08,540 So, this is not going to work. 37 00:03:08,540 --> 00:03:11,730 We have to instead say, 38 00:03:11,730 --> 00:03:15,795 from the current directory, which is myProject, 39 00:03:15,795 --> 00:03:19,390 you've got to go up a level to get to 40 00:03:19,390 --> 00:03:24,070 all projects and the way to say that you go up a level is to say dot dot. 41 00:03:24,070 --> 00:03:28,519 Dot dot says go to the containing or the parent directory, 42 00:03:28,519 --> 00:03:33,045 within all projects, we have to go down, 43 00:03:33,045 --> 00:03:35,115 descend into this sub-folder, 44 00:03:35,115 --> 00:03:39,850 myData, and we got to go down there, 45 00:03:43,630 --> 00:03:48,905 and now we've given the directions go up to the parent, 46 00:03:48,905 --> 00:03:56,475 within that go down to myData and it looks like I forgot to capitalize the d, 47 00:03:56,475 --> 00:04:07,350 Let me fix that, and then now we're in the right directory and now we can say data2.txt. 48 00:04:10,850 --> 00:04:14,220 Everything else is just the same. 49 00:04:14,220 --> 00:04:17,785 So that's called a relative path. 50 00:04:17,785 --> 00:04:24,570 This part is called 51 00:04:24,570 --> 00:04:32,050 the path and then we have the filename. 52 00:04:35,500 --> 00:04:40,075 There's also a way to specify an absolute path, 53 00:04:40,075 --> 00:04:42,900 that is absolute meaning. 54 00:04:42,900 --> 00:04:46,665 Here's how to find this file on 55 00:04:46,665 --> 00:04:49,310 the computer rather than relative to 56 00:04:49,310 --> 00:04:53,075 the current directory that you're currently connected to. 57 00:04:53,075 --> 00:05:01,245 When you do that, you'll have a path that begins with slash. 58 00:05:01,245 --> 00:05:06,250 So it would be something like /User/presnick/myFiles, 59 00:05:15,250 --> 00:05:23,490 and you'd have to give the whole path allProjects. 60 00:05:25,780 --> 00:05:35,920 After allProjects, you would go to myData and finally the filename. 61 00:05:39,610 --> 00:05:44,149 I don't recommend using these what are called absolute 62 00:05:44,149 --> 00:05:53,050 paths because it makes your code and data not portable. 63 00:05:53,050 --> 00:05:56,300 If I use the relative path, 64 00:05:56,300 --> 00:05:59,900 and I take this entire set 65 00:05:59,900 --> 00:06:06,440 of folders and I just copy it to someplace else on a different computer, 66 00:06:06,440 --> 00:06:11,900 maybe somebody else's computer not /User/presnick, 67 00:06:11,900 --> 00:06:14,015 and I can still find it. 68 00:06:14,015 --> 00:06:17,015 So, generally, people prefer to use 69 00:06:17,015 --> 00:06:20,870 these relative paths because it makes their code and data more portable. 70 00:06:20,870 --> 00:06:23,710 You can transfer it to other computers. 71 00:06:23,710 --> 00:06:27,755 Now, if you've only been running Python in the Runestone textbook, 72 00:06:27,755 --> 00:06:29,840 you haven't had an occasion yet to use 73 00:06:29,840 --> 00:06:33,350 these file paths when opening files and don't worry about it, 74 00:06:33,350 --> 00:06:35,735 just make a mental note to come back to this video 75 00:06:35,735 --> 00:06:38,240 or the corresponding page in the book when you are 76 00:06:38,240 --> 00:06:44,170 executing in an environment with files grouped into directories. See you next time.