Python Tutorial: Pathlib - The Modern Way to Handle File Paths
34:51

Python Tutorial: Pathlib - The Modern Way to Handle File Paths

Corey Schafer 23.09.2024 68 483 просмотров 2 655 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
In this Python Programming video, we will be learning how to use the Pathlib module and see why it's now preferred over os.path. Thank You to the sponsor, Ekster Wallets! Please use code "SCHAFER" at checkout! https://partner.ekster.com/coreyms In this video, we'll be learning how to use Path objects, which uses an object-oriented approach to file paths instead of using strings like done previously when using the os.path method. Many code bases are switching over to using Pathlib and Path objects, so this is an important skill to master. Let's get started... Working with Files in Python - https://youtu.be/Uh2ebFW8OYM ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Python #Pathlib

Оглавление (7 сегментов)

Segment 1 (00:00 - 05:00)

hey there how's it going everybody in this video we're going to be learning about the path lib module and how to use it now this is used in place of the OS module for working with paths and many people have been slow uh to move over to using this but we're going to be learning about the advantages that it provides over using the OS module and why we should probably make that switch now we do have a sponsor for this video which is extra wallets so I'd like to thank them for sponsoring the video and we'll talk more about what they offer after this lesson but for now let's take a look at the path lib module and why it's now the preferred way to work with paths in Python so path lib was introduced to the standard library in Python 3. 4 and provides an easy way to work with file paths and the file system so what's the big difference between pathlib and the OS module well the OS module uh you're working with strings for paths which can get a bit messy especially when working between different operating systems but with path lib it represents file paths as objects rather than strings and this will uh better handle those problems a bit more and it's more intuitive uh readable and less prone to errors and I'm not just bringing this up as a cool python tip or trick uh there are some large code bases out there that have switched over to using pathlib uh so for example in the script that I have open here I have two examples of a line from a Jango projects settings file the first one is from an older d jeno project where they were still using the OS module and the second one down here is from a newer jeno project where they've switched over to using pathlib and we can see the differences here if we were to look at the first example using the OS module it's kind of hard to tell what's going on you have to break it down slowly and think okay so we're getting the absolute uh file path of this double underscore file and then we're getting the uh directory of that absolute path and then we're getting the directory of that directory and it's a bit confusing when we try to break it down like that but basically all they're trying to do is set the base directory to the main project directory which is two levels up from the settings file now if we compare that to uh path lib down here then all we are doing is resolving the absolute path of this uh file variable here and then we're getting the parent of that and then if any of this is confusing don't worry we're going to go over all this in the video but I just wanted to show you the differences uh in kind of how you break things down here we can see that this OS one is a lot longer and we kind of have to read this from right to left where with pathlib we're uh this is a lot shorter we're reading this from left to right and it's just more intuitive as to what's going on and we can see here that these comments with these Django files are different as well we can see that to build the paths inside a project that they want you to use os. path. jooin with this base directory um in this older example and in the newer example they just have this base directory for SLS subdirectory and again we're going to take a look at all this in the video I just wanted to show you some real world examples right off the bat of some larger code bases that have made this switch so now let's go ahead and jump into learning how to use pathlib so I'm going to go ahead and delete those examples that we have and now you can see that all we're importing here is the OS module and we're also importing path from path lib so we'll walk through using pathlib for some different things and we'll also see examples of when to use pathlib or when to use um the OS module or another module if it has a better use case so first of all let's just look at the current directory that we are currently in to do this I'm just going to print out path and that is CWD for current working directory so I'm going to save that and run it and then get my output open here and we can see that we're currently in this tutorial directory on my desktop now let's list the files and folders within this current tutorial directory so if we use path without any arguments then it will default to the current working directory which is uh this right here what we just printed out so if we wanted to iterate over all of the files and folders in this current directory then we could simply say 4 p in path and I won't pass in any arguments and by default that'll just be the current working directory and we can use iter dur and this will make an

Segment 2 (05:00 - 10:00)

iterator of all the files and folders in that current directory so I will just print all those out go ahead and run that and we can see that it shows us all the files and folders within our current working directory so we have a file. txt uh tutorial. py which is the python file that I'm running here and then this directory one and we can also see these over here in the sidebar so let me go ahead and take both this file one and this directory one and create specific path objects for each of those uh so since they're in my current directory I can uh simply say I'll comment this out for now I can just say uh my dur is equal to and A New Path object and we will just pass in directory one here and now I'll also do this for file so I'll do file one. txt and I will rename this variable to my file so the way that I've done this so far is by using a relative path since I'm only typing in the directory name and the file name with nothing else it's looking for those relative to my current working directory now we can also use absolute paths as well and we'll take a look at that in just a bit but for now let's look at some of the information that we can get from these relative path objects so first let me print these out so we can see uh the output from both of these if I just print both of these out I will print out my directory and I will also print out my file and right now if I save this and run it then we can see that it gives the name of both of these now if I wanted to be explicit then I could go ahead and print out uh the name attribute for both of these so I'll go ahead and do that if I save that and run it then we can see that we get the same thing here uh we are getting the name for directory for our directory and our name for our file now another common thing that we might want to do is get the file extension now if you're using the OS module you might remember that to do this with that module you would first need to split the file name and the extension and then grab the index of whichever one you'd like uh but with pathlib this is just as easy as getting the suffix attribute so I can say suffix and I'll do that for both of these here so if I save that and run it then we can see that for our directory we don't have an extension since that is a directory and for our text file we have this txt extension and to get the file name without the extension that is what's called the stem so to do that I'll go ahead and replace both of those with stem if I save that and run it then we can see that now we just get directory one and it didn't have an extension to begin with and now for our file. txt we're just getting file one without that txt extension okay so now let's say that we wanted to access a file within this directory one here now these paths don't actually need to already exist when we create these path objects so for example directory one is currently empty so let's create a path to a new text file within that directory so with the OS module the way that we would do this was with os. path. jooin but with pathlib we have the ability to use this for SL operator that's common on a lot of file systems so to do this I'm just going to create this down here I'll say new file is equal to and now I'll use this existing path object of this directory and then we have access to this for SL syntax here and then I can just say I'll just call this new file. txt and now let's go ahead and print this out with the others and I'm going to uh take off these stems here so I will grab all of those get rid of those save that and run it and we can see there that our new file is at this path within directory one now if you're used to using the OS module and you're not comfortable jumping directly into using this for SL syntax here for your file pass then you still have access to a method that does the same thing um so instead of using a slash we can use join path as the method so I can say my dur do join path and then we will just pass in that new file. txt and if I save that and run it then we should get the same thing here and we can see that worked and like

Segment 3 (10:00 - 15:00)

I was saying before uh these don't actually need to exist in order to get this information so this new file. txt doesn't currently exist on my file system but we can treat that path just like any other so if we wanted to see if these paths do exist then we could simply use the exist method so let me go ahead and do that at the end of all of these here I will add the exist method I will save that and run it and we can see that for our directory we get true that it does exist this we get true for this file one. txt because it does exist and this new file. txt we can see that we get false because that file actually doesn't exist on our file system okay so now let's maneuver over to looking more at absolute versus relative paths and this is where I believe that path lib really shines over the OS mod modle so first of all let's get an idea of where we currently are on the file system so first let's take a look at the Parent Directory of each of these paths so to do this I can just uh get the dot parent attribute so I will run parent on each of these and print those out so if I run this we can see that for the first two paths we just get this Dot and that means the current working directory is the parent of that path for our new file within directory one we can see that directory one is the parent of that file path and you can chain these together too so if I wanted to go up two levels with this new file then I could say new file. parent. parent if I save that and run it then we can see that now uh it goes up to this current working directory level as well and this is actually as far as the these will go since these are relative paths but maybe we wanted to know exactly where we are on the file system and have an absolute path to these locations so in order to do that we can get an absolute path and there are two ways that we can do this the first way is to use the absolute method and this parent attribute is returning the path object also so we can either use an absolute the absolute method on that parent object uh or on the original object it just depends on how we would want to do this so for example uh what I mean by this is I could either say uh my directory. parent. absolute and if I run that then you can see that now we are getting the absolute path to this path location or I could just run absolute on that original path lib object my dur absolute. parent if I save that and run it then you can see that we get the same thing now if I just wanted to get the absolute path of this directory and not the parent then I could just take that parent off there rerun that now we can see that we get the absolute path to that directory now another way that we can get the absolute path is to use the resolve method and this is the one that I used basically 99% of the time uh what this does is it gives you an absolute path but it also resolves any Sim links or relative directory references uh so for example if I change this to resolve so let me go ahead and do that and I will save that and run it and we shouldn't see any differences right now and we don't it just looks like the same absolute path but if there were any Sim links or relative directory references in here then we would see a difference uh so for example let me comment out what we have here and let me create a new path to print out here so I'll say p is equal to path and I will make this path dot and then I will do uh dot absolute at the end of that and then let's just print out that absolute path so if I save that and run it then we can see we're getting this weird thing where it's taking us to the directory of our tutorial here and then it has this dot directory and that's not really what we want so this dot here is a common uh relative directory reference uh that usually means The Parent Directory but using this absolute method isn't resolving what we uh mean here by using that Parent Directory so what we need to do is instead use resolve so I'm going to change this absolute to resolve I will save that and run it and now we can

Segment 4 (15:00 - 20:00)

see here that it knows that what we wanted here was our Parent Directory and since we were already within uh this tutorial directory on my desktop then it knows that the Parent Directory that I'm after is my desktop itself so I found that for most of the time for what I need I almost always want to resolve paths when getting their absolute path and if we recall this is basically what we saw in the beginning where we looked at how D jeno changed their code base to using pathlib instead of the OS module so what they were doing was using the uh double uncore file variable which is a special variable that contains the path to the current file and then they were getting the absolute path to that file and they were using the resolve meth method as well so I could do that here uh by saying instead of uh dot I will use this file variable and I don't want to put this within quotes because this is actually a special variable here so if I save this and run it then we can see that we are getting the absolute path uh to this specific python script so if you ever want to get the Parent Directory to a specific file like they were doing in those Django setting files uh then hopefully now you understand this a little bit better of what they were doing they were taking the current file they were resolving that absolute abute path and getting that absolute path and then they were just getting uh the parent of that absolute path and in this instance this is going to be the directory that contains my tutorial. py file that we are currently in now in their example uh their project directory was actually two levels up so they put another parent there uh but the concept is the exact same now there are a couple more caveats about uh getting an absolute path that sometimes resolve won't take care of either uh so for example let's say that we wanted to get access to a user's home folder uh so for example I keep my DOT files and a lot of system settings in my DOT files folder of my home directory so a lot of us may be uh used to using the Tilda to access a user's home folder uh so let me uh try that with a path so I'm going to remove this parent for now and now let's say that I wanted to make this path to my home folder and I'll do a Tilda sl. files and this is going to be used as my uh users home folder here now if I save this and I run it then we can see that even with resolve that doesn't work like we'd expect it just added this on to the end of our current working directory so instead if we are parsing a string with the Tilda then you can use the expand and user method to get the correct path so instead of resolve here I will say do expand user and if I save that and run it then we can see that works it goes to uh users corms which is my home folder uh and then goes into this files directory and just like before if we wanted to then we could chain the resolve method onto the end of this uh if there were any Sim links or relative references in that path now I'd only use this expand user method if I was parsing an already existing string if I was creating this from scratch then it's much better in my opinion to use the home method so if I wanted a path to this files directory then instead I could just say path doome and then we can use that for slash notation and just go straight to the files directory there if I save that and run it then you can see that gives us the same thing so since I have this home directory here uh let's say that we wanted to search this dot files directory so we saw earlier how we can use the iter dur method to list all the files and folders in our path but it's pretty Limited in terms of searching and also you need to do some extra work in order to get that information from within the subdirectories of our path so in order to do some more advanced searching we can use the glob method so let's say that I wanted to search my do files path for any file or folder that has a VSS code in the name to do this we can use some wild cards with the glob method so really quick let me clean up uh what we have here so far um up here you can see that is where we uh looped over that itter dur method from before but let me go ahead and clean this up and I will actually just call this do

Segment 5 (20:00 - 25:00)

files and now let's search this using that glob method so I can say 4 P in. files. glob and again let's say that we wanted to uh find any file or folder that has VSS code in the name so what I can do here is I can use some wild cards and just put vs code here in the middle so it can be at the beginning or end in the middle that's what these wild cards here are so anything at the beginning and anything at the end uh as long as it contains this VSS code so now let's just print out all of those paths uh that match that so I will run that and you can see that we only get one result here for this VH Co VSS code. SH now there could actually be some more matches uh but by default glob only searches the path directory and not the subdirectories so to search the subdirectories we need to use recursive glob which we can uh use just by adding an R before glob here which stands for recursive glob so oh actually and I almost forgot uh we're still not guaranteed to get all the results that we might be after here because glob is also case sensitive which is sometimes what we want but in this case I don't mind if there is a mixed Cas uh I just want all of the results so I'll add an argument here to this recursive glob and I can say uh that I want case sensitive equal to false so now if I save that and run it then you can see that now we are getting some more results here and these have some mixed cases in them uh and they are also within the subdirectories here and you can use these wild cards in different ways so if I wanted to instead find all of the Json files then I could just use a wild card at the beginning and then say uh anything that has a Json extension I'm not going to put a wild card at the end since a we want a anything that ends in a Json so if I save that and run it then we can see that now we get all of the Json files in those dot files now let's say that we wanted to open one of these files in order to work with them uh so we can do that just by passing in a path lib object into the open function uh just like you might be used to uh doing with strings so for example if I wanted to open this VSS uh settings. Json file here let me go ahead and just add this to my path so I'm going to copy this settings uh vs code and I will just add this on here at the end and now instead of searching over uh using glob let's go ahead and open this up now to open this file we have a couple of different options here the path objects actually have an open method of their own so I could say with files. openen and then I can say as F and then let's just print f. read to get the contents of that file and by default that's going to open that in read mode so I can just run this as is and this is my VSS code settings. Json file down here so it did uh get the contents of that file and if we wanted to use the more traditional method of opening files then we can also just pass the pathlib object into the open function instead so instead of saying files. openen I could say open and then pass in dot files so if I save that and run it then you can see that we get the exact same thing here now I'm not going to go in depth on how to read and write files in this video because I have a separate video completely dedicated to that topic that I'll put a link to in the description section below uh so if you are interested then you can give that a watch but the big thing to know here is that we can use these pathlib objects with these open functions directly uh without need to convert them to Strings so that's extremely useful okay so now let's look at a couple more things that we can do with path lib and also we'll see a few examples of where using path lib isn't the best approach and when using some other modules might be better so with pathlib we have some limited ability to create move and delete files and folders so let's take a look at some of these so let's say that I wanted to create a new directory so to do this I'm just going to delete what we have uh so far and I'm going to create a new

Segment 6 (25:00 - 30:00)

path here and let's just specify a directory that doesn't exist yet I'll just call this uh temp directory so in order to create this directory I can use that path and then use the make dur method there if I save that and run it if we look over here in our sidebar then we can see that it creates that new temp directory now if this directory had already existed when I I'd run that then I would get an error but there's an argument that I can add to this uh makeer method of I think it's exists okay is equal to true and if I was to specify that then even if that directory already existed then it would not throw an error okay so now let's say that now that we have this directory that I wanted to remove it so to remove this I can just say RM dur on that path if I save that and run it then we can see that now it deleted that directory from our sidebar now one caveat here with removing directories is that this will only remove empty directories if you want to remove a non-empty directory then you'll have to use another module like the Shu till module uh in order to do that now if we wanted to create subdirectories as well then we would have to specify that we wanted to create all the non-existent parent directories along the way so for example if I wanted to create our temp directory plus another subdirectory then let me go ahead and add that in here let's say that we wanted temp directory and this subdirectory here then I could say make dur but if I run this now then it won't work because temp directory doesn't exist what I have to do here is I have to say uh parents is equal to true and that will say go ahead and can create any of the parents that you need along the way so now if I save that and run it then we can see that we have temp directory over here in the sidebar and if I click on that then we have subdirectory uh within there as well now like I'd said before uh you can't delete non-existent directories so if you wanted to delete this temp directory then you'd actually need to delete this subdirectory first and then the temp directory uh or what you could do is use the shu module which allows you to delete non-existent directories so now we've seen a little bit of uh how to work with directories uh but we can also create files as well so to do this let me go ahead and create a new file here I'll just call this temp file. txt in order to create um this file path here what I can do is just say path. touch and when I run touch on that object if I run that then we can see that now we have this temp file. txt over there in our sidebar and if that file had already existed before we ran that um touch then it wouldn't overwrite it would just update its modification time okay so we can also rename a file or a directory using either the rename or the replace method so if I wanted to rename this temp file to be lowercase then what I could do is on the this path object I could do a do rename and it's going to rename this path object here and I will pass in let's say that we just wanted this to be lowercase so I'll do temp file. txt if I run that and over here in the sidebar we can see that this is now a temp file all lowercase now rename can actually be a little bit tricky on some operating systems like mac and Linux it will overwrite existing files on Windows I believe that you'll get an error if the file already exists um I don't like to leave things like that up the chance so what I usually do if I want to be careful is I can always use a conditional to check if the file already exists uh like we did earlier in the video with that exist method now if you wanted to be more explicit that the file should absolutely be overwritten uh regardless if there's a file or directory already there then you can use the replace method instead of rename and that should work the same on any operating system so I usually use replace instead of rename uh since I do those conditional existence checks myself uh I don't like to rely on a method to do that for me so if I wanted to use replace here then all I would need to do uh since this is named temp file lowercase now let me change our existing path to be lowercase there let's say I wanted to uh replace this

Segment 7 (30:00 - 34:00)

with tempcore file instead of rename here I can just say replace and if I save that and run it then we can say that uh replace works as well over here in the sidebar we have _ file. txt and if you don't have any checks in place then that will definitely replace that file or folder uh no matter the operating system so be careful with some of the commands because some of them will overwrite existing data okay so uh lastly if we wanted to remove a file then we can use the unlink method so if I wanted to delete this temp file. txt again let me replace our existing path object up here with what we rename that to so if I wanted to delete that temp file then I could use unlink so if I save that and run it then we can see that file was deleted from our sidebar here so it is deleted from the file system okay so we're just about finished up here uh I still want to go over some scenarios where we wouldn't want to use pathlib and instead use Os or another module like sh util but first I'd like to thank the sponsor of this video and that's extra wallets so I've actually been using an extra wallet as my daily wallet for about 8 months now and I can tell you that I've really enjoyed this wallet as a replacement from what I had before the one I specifically have is leather but they come in a lot of different styles and materials their newest pro model comes in 100% aluminum and all of their wallets allow you to access your cards super easily just by pushing a button one of the things I've found most useful is their finder card add-on which is a super slim uh credit card size tracking device that's easily rechargeable and one charge can last as long as uh 6 months they have ADD ones that also allow you to add Apple Air tags to their wallets but I find the slim design of their tracking card much better since I don't even notice it in my wallet I'll leave a link to their page in the description section below and if you use code schaer at checkout then you'll unlock some extra savings on top of any current uh promotions that they're already running I'd love it if you all could go show some love to our sponsors and again I'd like to thank extra wallets for sponsoring this video Okay so we've seen why we'd use pathlib instead of the OS module and how it can be more readable and useful in a lot of different scenarios but when should we still use the OS module or another module like the sh util module that we've mentioned a few times before uh well there's a few scenarios so like the name hints uh path lib should be used for paths so when you're actually doing things on the operating system like getting environment variables uh the OS module is still what you want uh to use for things like that and like I mentioned previously pathlib might not uh offer some more advanced features uh that you're looking for like removing non-empty directories or copying files uh so for that you'd want to use something like the shutil module um I've not done a tutorial on the shutil module yet but I do plan on putting together something on that in the near future uh so we can see that what that's actually capable of but for the most part I've switched over to using pathlib and all of my newer projects uh for working with file paths and personally I think that you should too uh since a lot of the larger code bases have made that switch also it might feel a bit strange once you first start using it but once you get used to it definitely feels more intuitive and easier to work with uh than the OS module so I definitely think that you should give it a try uh but with that said I think that's going to do it for this video hopefully now you have a pretty good understanding of the path lib module and why we want to start using this in our code uh in some upcoming videos I'm going to be getting back to some web development Frameworks and releasing a new series on flask and Django so be sure you're subscribed to be notified about those but if anyone has any questions about what we covered in this video then feel free to ask in the comment section below and I'll do my best to answer those and if you enjoy these tutorials and would like to support them then there are several ways you can do that the easiest ways is simply like the video and give it a thumbs up also it's a huge help to share these videos with any one who you think would find them useful and if you have the means you can contribute through patreon or YouTube and there are links to those pages in the description section below be sure to subscribe for future videos and thank you all for watching

Другие видео автора — Corey Schafer

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник