Using Pathlib in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back everybody so today uh what we're going to be doing is we're going to be talking about a specific python library uh that specific python library is called path lib so what's the whole purpose of this video why are we even talking about this library there's so many libraries in python why dedicate a video to specifically this one well something that you're going to do a lot in your python programs is you're going to be working with paths file paths url paths all sorts of different paths and so one of the big challenges at least when i first started working with python was it was okay to work with pats when it was only on my system because i knew my system was windows and so i could always kind of know how to build a path and i always knew how to modify a path in order to make sure it was compliant with windows so the windows operating system however when i started expanding my code and having other people use it all of the sudden i ran into this issue not everybody is on windows some people are on linux some people are on mac and all of these different systems might have different roles for building and constructing their file paths and so what path lib offers you is the capability to basically make that process of ensuring your path works on both windows and mac and linux and all that fun stuff it makes it where it does the heavy lifting for you so you can define your path once and then what path lib will do in a lot of situations is ensure that your path is compliant for that particular operating system this makes it where when you're working with paths you don't have to worry about ensuring that oh well is this one that one or do i need to change it to make sure it works with this folder and all sorts of different stuff so it just makes that process a lot easier and then on top of it it makes the operations with paths even easier so doing things like hey i need to check if the directory exists i need to check if a file exists oh i need to see if this file belongs to this directory so asking and helping you ask all those questions that are inevitably going to come up when you're working with a program i can't tell you how many times i've had to check if a file exists i have to make sure a folder exists and then if it didn't exist i had to create it or if a file didn't exist i had to create it or here's another thing what about the extension type so what type of file extension is it that might make your program operate really differently so path limb offers the functionality to help answer those questions and then also do operations common operations with your paths so what we're going to do in this video is we're going to just kind of explore this library a little bit and then we're going to show some common operations and lease operations that i've used quite frequent frequently i'm not going to say we're going to cover every single method and property but some of the ones that i have found i keep using over and over and over again on top of that i might be jumping between the documentation and the code that's if i want to go through a little bit more of an in-depth discussion just so you understand certain concepts alrighty so let's jump into visual studio code if you haven't you want to make sure you create a new file so just again right click do new file and then you can call it something like using path lib or whatever the heck you want right so the first thing that you're going to want to do is you're going to want to import the path lib library now this is a standard library so if you install python guess what you already have it you don't have to do any installations it's a standard library however if you are using an earlier version of python 3.4 or lower then you would not have path lit path lib was added in python 3.4 so if you are on python 3.3 and 2 and all that you probably don't have access to path lib so this is a newer library this was really intended to i'm not going to say replace the os library but it was helping alleviate some of the challenges of working with that particular library so that's one of the big caveats with using this library so you do have to be on python 3.4 or above so let's create just a basic path but more specifically i want to create a path to the script that i'm currently writing and so this using path lib video to give you an idea of what this full path looks like i'm going to right click a new copy path and so i want to have something like this i want to create a path to this particular file so how do we do that well let's just do this so we'll say let's create a path to this script now we're going to do print because we're going to print out the path i'm going to call my path lib library there's an object called path there's actually quite a few different path objects but we'll talk about that a little bit and i can call a special dunder method which basically allows us to reference this particular file so if you do double underscore file it will return the path to the script that you're basically running so if i right click and then i do run python file in terminal you'll notice i have it right down here so it looks almost identical certain things are lowercase and stuff like that but for the most part it was able to take this particular script and put it in a path object so very useful i've done this a lot especially when i'm doing things like i might be adding json files to a particular library to store certain state information i find i'm often using this particular line of code quite often so definitely want to use that as your notes now like i said before if you looked into it you would have noticed there's different types of path objects and you're kind of going like why are there all these different types of paths right because if you look there's pure path uh windows path i don't know how to say that one pure postings i don't know there you go again so what are what's the difference between these different types of paths well to basically put it at a high level is some of these objects are purely comp you know computational operations so without the actual input output operation so it allows you to kind of in a sense mimic doing certain operations if you're not on that operating system so let's say for example that i'm on linux but i want to work with a windows path right well this would allow you or these different types of path objects would allow you to mimic the operations that you could do on windows while not being on windows however you're gonna find there's gonna be things like you can't make a directory you can't create new files it's purely hey i need to change a file path the structure of it um or i need to do something like get the parts of it or something like that but you're not doing the actual like operational like creating a file and creating directories right because you're not technically on that operating system so you can't call those operations if you're not on that operating system but sometimes you need to test things and so this is where those pure paths come into play so to give you an idea what's the difference well let's say i want a windows path right so i want a windows path well in this case i would do path lib windows path and then i want to do a file right so a windows path is an actual path object that i could do windows input output operation so this is like this one but it's more specific it's actually just saying hey it's not this generic path object it's a windows path object and then we have windows peer path so this is like a windows path but i am giving up so i'm taking away those input output operations so i would say path lib and then it would be um path lib and then pure windows path now when you print these out they're going to look identical okay so they're gonna look identical so i'll put windows pure path and then i'm gonna do uh string windows path so they're gonna look identical so you're kind of going like what's the difference right well let's just take a look at some of the methods so windows path so you'll notice here where is it you have operations like make directory this actually will create a new directory open if you need to do things like rename if you need to do things like touch create a new file so you'll notice that there's more operations but if i do windows peer path whoa all of the sudden look at this i don't have those operations right so you still get certain operations right so you can still get the parts of a path you could find out the parent structure of a path but i can't create a directory with a peer path so i don't get those input output operations i do not get that capability to actually run those instead i get the i it's kind of hard to explain to be honest it's just one of those things where i'm trying to figure out the best way to explain it but it's more of this idea that you can manipulate the path but you can't actually do an operation with the path but i want to be cautious about saying operation because technically these are operations like you're joining multiple path but you're not able to do the actual writing operations of that path so that's that's really the difference between a peer path and a just regular path object now if you look at the documentation you'll notice here there's path which a lot of the times that's probably the one you're going to be doing and then each one of these are basically can be the pos ix path and then the windows path so these actually have the operation so these three right here have the operations the input output operations and these three at the top only have the operations where you can manipulate that actual path but you don't have that writing input operation so because i'm on windows i really wouldn't be using this one a lot in certain situations i might need to but a lot of times i would be working mostly with windows path but even then i really just work with path because path basically just does all the hard part for you it just makes it easier so really important you understand that it's a little bit kind of odd at first you're kind of like oh like why are these different paths and a lot of it again it's just to make working on different operating systems easier now one property that is nice is called parts so we'll show you what um grab the parts of a path so let's take a look and see what parts return so windows path and then we're going to do parts what's this all do oh well that's kind of handy it will break your path into its different parts basically it's kind of like different directories so where might this come in handy well what if i need the file name with the extension super easy then you could just call parts and say hey give me the last item right give me the last item in this one perfect so it makes grabbing the different parts of your particular file path easier so super helpful i've used that one a ton there's another one called parent now parent is useful because maybe i want to get the folder above the one that i'm currently in right so i'm i have this script file and i want to get the folder that that script file is in but what i could do is i could call my parent method or sorry my parent property and that would return the the parent of that particular path object so basically the the directory that it's in so in this situation okay this returns that particular that particular folder that it's in well maybe i want the one that's above that one right so maybe i want lessons hyphen python well how do i get that one super simple i'm going to do windows path and then i'm going to do parent dot parent and then you'll notice it gives me that one but there's a problem with this what if you have a really nested directory i mean i'm talking like maybe you're going 15 or 20 folders deep right i'm not going to judge but you know you're probably going super deep at that point so it kind of depends on what you're trying to do however you don't want to do this 20 different times it doesn't make a lot of sense well guess what there's an easy way to get around that you would do windows path you would do pair rinse so s at the end and then it's kind of like slicing a list right so you would say well which level do you want to go up so zero is start basically at this one so sorry not that one this one and then you'll see so this one returns that if you go one you go one level higher and then if you do two you go the next level higher so parents is great when you're dealing with a very deep hierarchy so very nested hierarchy and you're not necessarily sure how deep it goes or you just don't want to write out parent 20 different times i remember before i i learned about this property there was one time i did it like four times i'm like there's got to be an easier way of doing this lo and behold it was this one so really really helpful i use that property a ton a ton a ton a ton and then on top of that you can also join paths together right so sometimes you might have a a path to a directory and you want to join that path with that directory with a file name so that often happens a lot where you want to build the entire file path but you only have two pieces you've got the directory file path and then you've got the file name with the extension at the end so how can you do that well let's kind of run through an example we'll call this one data folder and what we're going to do here is i'm going to take path lib dot path i'm going to do my file again and then i'm going to do that little property we just did before i'm gonna do parents i'm gonna do the one level above so we'll start at zero and then i'm gonna call this method called join path and then i'm gonna attach the next component that i want to do it now here i'm just saying hey add kind of the component data to it but i could make it even more structured i could say add data and then reports and then august reports right so you could make this pretty deep if you wanted to but again i just want to do one level below it so what would this look like well let's take a look and see what we get okay so now we have our particular file path but now we've got this data component attached to it super useful now what i'm going to do is i'm going to put video at the end of it because i already have a data folder and i don't want to overwrite this one but i do want to basically show you how we can create a folder from our script because sometimes we do need to create directories for whatever reason so what i'm going to do next is i'm going to say if not i'm going to call this data folder object and there's a method called exist well what does exist do if it exists so if this kind of folder exists it's going to return true if it doesn't exist it's going to return false so all i'm saying here is if this is false then take that data folder object and call the make directory method and that will create the directory for you so if i run this i now have a new folder called data video super useful so now what i'm going to do is i'm going to take my data folder i'm going to call join path again so again super useful i can just keep taking this and basically appending on each little component that i need to add to it and now in this situation i'm going to add a file name plus its extension so this is a text file called my file text and what i'm going to do is i'm going to say touch touch basically creates that new file now there's some other arguments you can pass through and one of them is called exist okay and so basically what happens is if it exists it will simply overwrite it now sometimes that's what you want to do sometimes that's what you don't want to do so exist is useful if you don't care about overwriting the file so if you set that to true you're basically saying i don't care if i overwrite the file if you put it to false you're saying i care if you overwrite the file so it's going to return an error if you set it to false and that file already exists so super useful as well now it doesn't have to be just text files you could do csv files you could do json files you know there's lots of different things you could do with it so super nice right in fact you can even put another folder inside of it there's nothing stopping you from putting another folder inside of an existing one so you can start seeing that this is really i would say a nice when it comes to just building out certain things like your project directory so i've done this before too where sometimes i just need i'm not really sure if the user is going to set up the directory correctly so i try to take on that responsibility for them and i'll set up the folder structures for them and so this will help me do that so let's do this and see what we get okay and now you'll notice there's another folder there's a json file and there's a text file super super useful use this a ton i love this library but oh before it oh man it was hard sometimes okay let's talk about iterating through a folder so we'll say for file object in data folder there is a method called iter directory and basically what that does is it will iterate through a directory for you and it will go down each level so if there's a folder it will loop through that one if there's a folder inside of that one it will loop through that one and so on and so on and so on so again depending on how complex your folder is i mean if you're talking 20 layers deep you know you just have to kind of be careful with that you might get a lot of output that you're not necessarily expecting so what you would do is you would loop through your particular folder and then you take your file object and it's another basically path object and you can call some of the same properties again so what i'm going to do is i'm going to say print file object and actually have some code that i'm just going to kind of copy over just because a lot of it's mostly properties and nothing super crazy we'll just kind of talk through what these properties do when you might want to use them and stuff like that so um a lot of times you're asking questions about your file path object that's honestly how i think about coding a lot of the times is you're asking a question about a particular object um in this situation i'm asking the question you know is this file path a directory it's very helpful known uh it would be the is uh directory method and then are you a file so again is file a lot of these are relatively i would say intuitive like if you look at okay is file it's probably asking if this particular object is a file um is it a symbolic link i don't use symbolic links a ton i have some people who do i just haven't really had to recently um is it rel is relative to so for example if i want to see if this data folder is or if this file that i'm currently in is it relative to the data folder so basically does it exist in the data folder that should be true in this situation because all of these are in the data folder uh file drive so you can get the drive that exists the stem this one i believe is the actual file extension if i remember anchor i'll see what that is in a second name name of the file suffix uh no i think this is the file extension it's these two i can never remember which ones but we'll see in a second so bear with me perfect here we go alrighty so you can see just returns a lot of different properties about it so answering questions file drive file stem so this is the file name suffix is the extension so basically if you take the stem plus the suffix you'll get the name anchor is basically the drive with your little forward slash and that's really about it so again i've used this a ton just where i have to ask certain questions about my file objects a lot of times i'm going to be using these different properties okay uh let's kind of start talking a little bit more about just useful properties or methods so uh one of the ones that we've already talked about was grabbing the path to the particular script so the double underscore file that is really useful if you need to get the file path of the script that you're basically running or writing from so that's super nice another one is grab grab the current working directory working directory so in this situation print string path lib dot path uh current working oh sorry path current working directory so in this situation you don't technically need to pass through a path you can do it where it's not outside of it some people ask you know why are you putting string around it so a lot of the times when you're printing it's not an issue it will be an issue if you are in a situation where you're like storing it in a json file then in that situation you need to make sure that you actually wrap it as a string because certain things it's not technically when it's printing it it's looking like it's a string but it's actually an object so you just need to be careful of that so that will grab the current working directory um another one is the home path so if you need uh the one to the particular user's home path so path lib dot path and then this one is home the other one that is super well we'll print out these ones just so you can see it so this is the current working directory this is my home directory so c users alex i've used this one a lot too so especially if you're storing things like in temp folders or if you're using it in a system folders you're probably going to be using your home method an awful lot now what are some other things well there's this uh what is it trying to think what it is so then there's this method called absolute and so this one you kind of just have to see it to kind of understand what it does so let's take this path and do this one called data okay so you can see here there's it's just literally data it's not attached to this folder or anything like that but what you can do is you can call the absolute method and it will basically construct the entire path for you so it saves that headache of oh sorry i misspelled print apologies so you'll notice here oh sorry not data date okay but you can see here it was just basically a string i mean it's it represents a path but it's a path to nowhere right now but if i call the absolute method it basically assumes that okay he's talking about the current directory that he's in so it's like you're attaching the current working directory with that particular one so again useful sometimes i can't say i've used that a ton only because a lot of times i would already know where it's at i guess it's more i this one i could see being useful if you don't know what the user is potentially doing i could see where this might be helpful um there's also another method called resolve so this one comes in handy when you're working with paths that are partially uh what's the word not completed so they're parsley not completed completed i don't know why that's setup.pie so that one's this what we'll look at this one and then we'll do resolve so it's basically going to fill in this extra stuff so you'll notice it it filled in uh basically everything above it so it's like again it's basically taking the last part and attaching it to the current working directory you know can't say i've had a situation where i've done this one but you can also use resolve with another issue which is hey i need to take a windows path and convert it to a linux path that one's a common one let me do this path lib.path um let me do this copy path and this one i gotta change it a tiny bit so bear with me okay okay so right now it's technically like a linux style one but if you call resolve guess what it's going to do oops you'll see in a second so right now it's kind of like in that linux style oh now it puts it back into the windows style so again i'm on windows so if i call resolve and you had passed me a linux style if i call resolve it's going to say okay make sure it's compliant with ever whatever operating system we're currently on so this is super useful again when you're taking a linux path and you need to convert it to windows or you're taking a windows path you need to convert it to linux and so on and so on so again very very useful alrighty and so technically with that that's what i would say is probably the most important stuff there's some other stuff about you know getting certain statistics about a file like the size of it um the make time and stuff like that you know those are kind of specific i'll put them on the one that i'm going to post to github but for the most part this is what i wanted covered was just showing you common operations common properties that i have found useful with using path lib very very useful library i encourage everybody to read into it a little bit more just to make your life 10 times easier alright so if you have any questions feel free to put them down in the comments below in our next series what we're going to be doing is we're going to be talking about getting started with python so this has been a highly requested topic which is alex i'm new to python how do i get all set up so things like vs code get get bash github googling error messages tracebacks all sorts of fun stuff so it's gonna be a couple videos but it's just little stuff that you know when you're new to coding trust me it's all confusing in the beginning and hopefully those videos will take away some of the confusion alrighty well we will see you all in the next video
Info
Channel: Sigma Coding
Views: 4,084
Rating: undefined out of 5
Keywords: Current Working Directory, Home Path, JoinPath, Make Directory, Pathlib, PosixPath, PurePath, WindowsPath, Working with Paths in Python
Id: DOgjN7RmHds
Channel Id: undefined
Length: 29min 33sec (1773 seconds)
Published: Mon Mar 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.