 one of the things that we may be doing when we're working on code is Generating data obviously, but we may want to walk away from that Program and still have that data for a number of reasons. Maybe we're not walking away from it But we're taking that data and we have another program that we need to pass that information into but you know, we Don't we didn't build it. It's not code or something like that. It's like a gooey Either way, that's where we get into File handling and the entire idea is if you've ever played a video game, that's what saving your game is You're creating something that would be considered Persistent data I need to take sort of the values the ones and zeros the numbers or the strings and Anything that I have in my program and I need to put it into a file And so that allows me to then again go and use it later on or you know Just have a copy of that elsewhere outside of my program because again if my program closes Then it's removed from memory. So when we start dealing with files The first thing that we have to understand is something known as file location and even before we get to relative The first one I want to focus in on is something known as absolute file paths Now the idea behind this is regardless if you're on a windows a Mac Linux machine you have something known as a root directory All of your program files all your programs all of your music everything It's all sitting somewhere inside of the root directory and the idea there. I use that term Directory the entire idea is this is where we get the concept of Folders so the root directory for example is going to happen to have a Number of Directories as well and if I'm dealing with something like absolute file paths What I need to do is I need to explicitly start with my root directory. So that's For the windows and then that's for a unix-based system. So, okay, let's even see this in action If we take a look at a command prompt I've been working off of the CSC 111 folder that I built sort of at the beginning of this course and You may notice that there's a number of different elements going on here There's that C drive going on and colon because it needs to specify what root directory if you happen to have Multiple hard drives and whatnot whatever but then you see users atom desktop 111 And if I go and type in dir I can see that there's even more so literally There's the template notebook that you work off of there's a demo py and then there's each week the project Images data. There's just a number of different directories inside of these and if I were to Go in and see the change directory to data You'd see that there's something like iris or readings that TXT Now when we're dealing with absolute file paths, the way to think about this is I'm being Explicit to the point of saying starting from all the way in the root directory This is the thing I'm referencing and so a way to think about this if I go in and do readings dot txt You're gonna see that it pulls up that file and so it's been opened up and you see it's a bunch of numbers We'll talk about those later on But okay that was because I'm in that root directory, but what happens If I'm not in that root directory, oh, I don't you can see there's nothing there that I can Refer to so the idea behind absolute file paths is to very explicitly go all the way from again root directory to that file So if I wanted to see for example the readings dot txt file again because I'm processing it or I'm editing it I'd need to come in and go, you know in my case root directory of see users Adam Desktop not debug Desktop see a C111 data Readings dot txt Now that I've put all of that in oh I can see the data once again But that's where we also now get into the idea of relative file paths because Realistically you are not going to have a folder on your computer called Adam Unless your name's Adam, but you don't count Either way the entire idea is well if I try to reference say for example users Adam Readings dot txt and I don't happen to have an Adam's folder Well, my code is gonna crash because again Python doesn't know that folder. It's like oh, I can't see that so nothing's there When we think about a relative file path, what's going on here is now we're saying well, I'm going to just Use my surrounding environment to reference some material So the same kind of concept goes into play if for example instead of going CD If say for example Again, I'm sitting in sort of my route or not route director I'm sitting in something we would call a parent directory. I again have that data Folder that is sitting sort of inside of me. Well when we think about relative file paths Instead of going in and typing all of this information of absolute file paths Instead, I could just reference. Oh I have a folder data and I can reference readings at txt From it and so that's where this idea of a relative file path can come in But say for example, I'm not looking at the folder from below me. Well in that case Let's say for example, we're in week oh six. I still want to reference that That readings at txt Well, that's where if you happen to notice if I were to Take a look at this line in particular There we are You might notice that you know every computer Regardless, you know windows Linux Mac if you list out the directories on a command prompt You're gonna see those dots dots and the entire idea is that this first dot References that I'm in this is my directory if I'm here, you know Dot refers to myself if I refer to dot dot then what I'm saying is oh go up a Directory and just to see that in action if I were to say if I were to There we are If I were to say CD dot dot change directory up a directory Oh, what do you know? I go up a directory. So again, that's just sort of referencing That I can have a way to go to a parent directory Well, again, if we think about this that parent directory has a data folder in it So if I wanted to reference readings that TXC Go up a directory which has the data file Readings that TXT Okay, so I've explained how files work, but I haven't explained how to load them into Python So let's take that exact same idea and put it into place again We're still working off of that CSE 111 file Structure that were our folder that we're working off of again if I'm thinking about this from the week six folder That's where I'm sitting right now and you can sort of see that's going on here. You know notebook desktop 111 week 06 That's a very important part So if I want to reference the file again I have two different ways that I could handle this the first one again, I'll start with the absolute file path And I'll call it ab abs file Is again, we're starting to focus on the idea of Where it is in relation to? The root directory well again if we are thinking about that just to cheat you see that it's in the sea users atom desktop 111 so again, I'm going to do a quick little cheat and Highlight that information Here we are slash Reading out data slash readings dot txt so again I've explicitly stated go all the way to my root directory and my computer has an atom dot txt or atom folder in it So it will be perfectly fine, but again, I've just referenced it all I've done is made a string that's it so How do I open it? Well glad you asked if I come in and I like to reference it I typically like to call this variable phi because file Sometimes is a special word for certain Programming languages you could also use file or in fi whatever But if I want to open a file open so from here I Need to specify two parameters. The first one is the file path or The file that I want to work off of The second one is asking whether or not I am reading from the contents of this program or of this file or Writing to them and we use a string character for this R stands for read W stands for write and a stands for append But to at least start we're looking at the idea of reading those contents Unicode, okay. Oh, yeah. Yeah So here's a fun little thing if you're working off of the back slashes because you're on a windows machine You might remember that these are Unicode characters, so it sees those slashes and it's like oh you need to tell me, you know The what's you need to give me a correct Unicode. Well, we're not dealing with Unicode This is actually some of the headaches for file paths But now if we take that and run it perfectly fine this idea here is the slash slash First slash again Python thinks that that is a special character Instead we're saying and no, it's not a special character. Just give me the Slash in a way to see this in action is if we print that You'll see that it completely ignores that it's like oh you you want the slash there. Okay, that's perfectly fine another way With the forward slash and pythons, you know a little forgiving here where it sees the different slashes and it's like, okay perfectly fine No error and it still works This a little easier as you can tell I don't need to do double slashes So pick and choose which way you want, but either way now I happened to have my file in my program, but the problem is as you can see it's Not incredibly helpful It's actually an object just like many things that we sort of have worked off of in our class But the entire idea now is well, I want to load those contents into my program I typically like to use a variable called contents Phi dot read you knows Oh look at that no errors and that's because That dot read is going to say take everything that is in that file read it in to this variable And you can see that that is exactly what we see. It's not pretty as you can imagine I can clean it up just a hair by printing it instead of Say display it But there are my contents now again What if we think about this I've had to use the absolute file path, but you may not have Adam on your Computer or rather you may not be working off of your desktop or you may not be using CSE 111 as a folder for your contents And so this is once again where the idea of relative file paths can come into play Once again, I'm in the week 06 folder. You notice pretty little Green to indicate we're in we're using this file handling demo file So once again if we think about it if I wanted to do relative file paths Instead of specifying all of this information The first thing I need to do is specify go up a directory Go up a directory Then that next slash is saying go down a directory. Here's that data folder go down into the data folder And there is the readings that txt so readings that txt So I take this I run it notice no errors going on there Take it we've made a nice little change. The file is still being processed the same way So we still have to read in those contents and we still have to print them out And so that is how well You do absolute and relative file paths