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


Talk Python's Mastodon Michael Kennedy's Mastodon