Python for Absolute Beginners Transcripts
Chapter: Reading and writing files in Python
Lecture: Demo: File location without assumed working directory

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The next thing we need to talk about
0:01 with our files is locating the file itself.
0:04 It seems easy, here's our game, here's our file
0:07 that's the name of the file, we try to load it
0:09 it works.
0:10 However, the reason this works is because
0:12 the working directory as far as the program is concerned
0:15 is this folder, so we can find this file in that folder.
0:19 There are many reasons that might not be the case.
0:21 We can force this to happen
0:23 if we go over there to the working directory
0:25 and we just take away rocks game
0:27 like this, and we run it.
0:29 Yikes, that's not loading up the rules
0:31 like we were hoping, was it?
0:33 And we got a file not found error
0:34 one of these exceptions and the details is
0:36 no such file or directory rolls.json.
0:39 So we want to be able to load this file no matter what
0:42 and the way we're going to do it is we're going to say
0:44 go to whatever this file is
0:46 get the directory of that
0:48 and then put it together with this file.
0:51 Now, we can get the directory
0:53 well, we can get the name of this file
0:55 using this little trick here.
0:56 Say local file, is going to be this.
0:59 It's just going to be dunder file.
1:01 We can print out local file real quick.
1:03 So let's run and see what we get.
1:04 We get the error.
1:05 Here you can see this is the actual file.
1:08 So what are we going to do?
1:09 What we want to do, well, we want to get rid of this part
1:12 and we want to get the rest of this here
1:14 this whole ginormous name
1:16 and then we want to append to the end this
1:19 and make sure there is a separator.
1:21 This is tricky in a couple of ways.
1:22 First of all on Windows this would be \
1:26 separating the directories in the file on macOS
1:29 and Linux is /.
1:32 There is other issues around this as well
1:34 that get really, really challenging.
1:35 If I'm going to combine the directory and the file
1:38 I got to make sure that there's a separator
1:40 either those two that I need
1:42 it has to be there or it's going to be like
1:44 the directory rows.json, which isn't great.
1:46 So there's a bunch of little juggling that gets tricky here
1:49 and luckily we don't have to juggle it.
1:51 There is a library for this, it's called os.
1:55 We import os at the top and we come down here
1:58 we can say the directory is going to be os.path.dirname
2:04 from the file.
2:05 I'll just use the file directly like this.
2:07 And that's going to give us the directory.
2:09 Let's go and print out directory.
2:12 We get the error.
2:13 We start to fix it
2:14 but notice now we've got just the directory.
2:17 That's a good start.
2:18 So the next thing we want to do is we want to get a file
2:23 it's going to be the directory with this
2:25 on the end of it but with a separator.
2:26 So we can easily go os.path.join and give it the directory
2:32 and that, and then let's print out filename.
2:34 And now it's going to be the right place
2:36 no matter where the working directory is.
2:39 Let's run it, it should work now.
2:41 Look at that, it's back.
2:42 It loaded it, and if we go and look at the file
2:44 here it is, look.
2:45 Even though we didn't have this one with the separator
2:47 or this one with the separator, oops
2:49 this one with the separator, it added that back.
2:51 On Windows that would be a \
2:53 on the other systems a /, on the POSIX systems.
2:57 Really, really cool that we can do this little trick.
3:00 And let's go like that.
3:02 There we go.
3:03 We don't need to print that stuff out.
3:04 That was just for us to see what was going on.
3:06 So we're going to go to a file though we know about this one
3:09 and we're going to say get the directory, just here
3:12 and we know that right next to it
3:14 in the same directory is this, so we just use this name.
3:16 You can also use like dot dot
3:18 to say go up for directory and things like that.
3:20 So it looks like our file loading is much safer
3:24 and it's always going to work regardless
3:26 of what the working directory is.
3:27 Tada.