Python Tutorial: Pathlib - The Modern Way to Handle File Paths — Transcript
Full transcript
- 0:00hey there how's it going everybody in
- 0:01this video we're going to be learning
- 0:03about the path lib module and how to use
- 0:05it now this is used in place of the OS
- 0:08module for working with paths and many
- 0:10people have been slow uh to move over to
- 0:13using this but we're going to be
- 0:14learning about the advantages that it
- 0:17provides over using the OS module and
- 0:19why we should probably make that switch
- 0:21now we do have a sponsor for this video
- 0:23which is extra wallets so I'd like to
- 0:25thank them for sponsoring the video and
- 0:27we'll talk more about what they offer
- 0:29after this lesson but for now let's take
- 0:31a look at the path lib module and why
- 0:33it's now the preferred way to work with
- 0:36paths in Python so path lib was
- 0:38introduced to the standard library in
- 0:40Python 3.4 and provides an easy way to
- 0:43work with file paths and the file system
- 0:46so what's the big difference between
- 0:48pathlib and the OS module well the OS
- 0:50module uh you're working with strings
- 0:53for paths which can get a bit messy
- 0:56especially when working between
- 0:57different operating systems but with
- 0:59path lib it represents file paths as
- 1:02objects rather than strings and this
- 1:04will uh better handle those problems a
- 1:06bit more and it's more intuitive uh
- 1:09readable and less prone to errors and
- 1:12I'm not just bringing this up as a cool
- 1:14python tip or trick uh there are some
- 1:17large code bases out there that have
- 1:18switched over to using pathlib uh so for
- 1:21example in the script that I have open
- 1:23here I have two examples of a line from
- 1:26a a Jango projects settings file the
- 1:30first one is from an older d jeno
- 1:32project where they were still using the
- 1:34OS module and the second one down here
- 1:37is from a newer jeno project where
- 1:39they've switched over to using pathlib
- 1:42and we can see the differences here if
- 1:44we were to look at the first example
- 1:46using the OS module it's kind of hard to
- 1:48tell what's going on you have to break
- 1:51it down slowly and think okay so we're
- 1:54getting the absolute uh file path of
- 1:57this double underscore file and then
- 2:00we're getting the uh directory of that
- 2:04absolute path and then we're getting the
- 2:06directory of that directory and it's a
- 2:09bit confusing when we try to break it
- 2:10down like that but basically all they're
- 2:13trying to do is set the base directory
- 2:16to the main project directory which is
- 2:18two levels up from the settings file now
- 2:22if we compare that to uh path lib down
- 2:25here then all we are doing is resolving
- 2:28the absolute path of this uh file
- 2:32variable here and then we're getting the
- 2:34parent of that and then the parent of
- 2:36that and if any of this is confusing
- 2:38don't worry we're going to go over all
- 2:39this in the video but I just wanted to
- 2:41show you the differences uh in kind of
- 2:44how you break things down here we can
- 2:46see that this OS one is a lot longer and
- 2:49we kind of have to read this from right
- 2:51to left where with pathlib we're uh this
- 2:54is a lot shorter we're reading this from
- 2:56left to right and it's just more
- 2:59intuitive as to what's going on and we
- 3:01can see here that these comments with
- 3:03these Django files are different as well
- 3:06we can see that to build the paths
- 3:08inside a project that they want you to
- 3:10use os. path. jooin with this base
- 3:13directory um in this older example and
- 3:17in the newer example they just have this
- 3:19base directory for SLS subdirectory and
- 3:22again we're going to take a look at all
- 3:24this in the video I just wanted to show
- 3:26you some real world examples right off
- 3:28the bat of some larger code bases that
- 3:30have made this switch so now let's go
- 3:32ahead and jump into learning how to use
- 3:34pathlib so I'm going to go ahead and
- 3:37delete those examples that we have and
- 3:39now you can see that all we're importing
- 3:41here is the OS module and we're also
- 3:43importing path from path lib so we'll
- 3:47walk through using pathlib for some
- 3:48different things and we'll also see
- 3:51examples of when to use pathlib or when
- 3:53to use um the OS module or another
- 3:57module if it has a better use case so
- 3:59first of all let's just look at the
- 4:01current directory that we are currently
- 4:03in to do this I'm just going to print
- 4:06out path and that is CWD for current
- 4:11working directory so I'm going to save
- 4:13that and run it and then get my output
- 4:16open here and we can see that we're
- 4:18currently in this tutorial directory on
- 4:21my desktop now let's list the files and
- 4:24folders within this current tutorial
- 4:27directory so if we use path without any
- 4:31arguments then it will default to the
- 4:33current working directory which is uh
- 4:36this right here what we just printed out
- 4:38so if we wanted to iterate over all of
- 4:41the files and folders in this current
- 4:44directory then we could simply say 4 p
- 4:49in path and I won't pass in any
- 4:52arguments and by default that'll just be
- 4:54the current working directory and we can
- 4:56use iter dur and this will make an
- 5:00iterator of all the files and folders in
- 5:04that current directory so I will just
- 5:06print all those out go ahead and run
- 5:08that and we can see that it shows us all
- 5:10the files and folders within our current
- 5:12working directory so we have a file.txt
- 5:15uh tutorial. py which is the python file
- 5:18that I'm running here and then this
- 5:20directory one and we can also see these
- 5:23over here in the sidebar so let me go
- 5:25ahead and take both this file one and
- 5:28this directory one and create specific
- 5:31path objects for each of those uh so
- 5:33since they're in my current directory I
- 5:36can uh simply say I'll comment this out
- 5:39for now I can just say uh my dur is
- 5:43equal to and A New Path object and we
- 5:47will just pass in directory one here and
- 5:52now I'll also do this for file so I'll
- 5:55do file one.txt and I will rename this
- 5:59variable to to my file so the way that
- 6:02I've done this so far is by using a
- 6:04relative path since I'm only typing in
- 6:07the directory name and the file name
- 6:09with nothing else it's looking for those
- 6:12relative to my current working directory
- 6:15now we can also use absolute paths as
- 6:17well and we'll take a look at that in
- 6:19just a bit but for now let's look at
- 6:22some of the information that we can get
- 6:23from these relative path objects so
- 6:26first let me print these out so we can
- 6:28see uh the output from both of these if
- 6:31I just print both of these out I will
- 6:35print out my directory and I will also
- 6:39print out my file and right now if I
- 6:43save this and run it then we can see
- 6:45that it gives the name of both of these
- 6:48now if I wanted to be explicit then I
- 6:51could go ahead and print out uh the name
- 6:55attribute for both of these so I'll go
- 6:57ahead and do that if I save that and run
- 6:59it then we can see that we get the same
- 7:01thing here uh we are getting the name
- 7:03for directory for our directory and our
- 7:05name for our file now another common
- 7:08thing that we might want to do is get
- 7:09the file extension now if you're using
- 7:12the OS module you might remember that to
- 7:14do this with that module you would first
- 7:16need to split the file name and the
- 7:19extension and then grab the index of
- 7:21whichever one you'd like uh but with
- 7:24pathlib this is just as easy as getting
- 7:27the suffix attribute so I can say suffix
- 7:31and I'll do that for both of these here
- 7:34so if I save that and run it then we can
- 7:37see that for our directory we don't have
- 7:39an extension since that is a directory
- 7:41and for our text file we have this txt
- 7:44extension and to get the file name
- 7:46without the extension that is what's
- 7:49called the stem so to do that I'll go
- 7:52ahead and replace both of those with
- 7:56stem if I save that and run it then we
- 7:59can see that now we just get directory
- 8:01one and it didn't have an extension to
- 8:03begin with and now for our file.txt
- 8:05we're just getting file one without that
- 8:08txt extension okay so now let's say that
- 8:11we wanted to access a file within this
- 8:14directory one here now these paths don't
- 8:17actually need to already exist when we
- 8:19create these path objects so for example
- 8:22directory one is currently empty so
- 8:25let's create a path to a new text file
- 8:27within that directory so with the OS
- 8:30module the way that we would do this was
- 8:32with os. path. jooin but with pathlib we
- 8:37have the ability to use this for SL
- 8:39operator that's common on a lot of file
- 8:42systems so to do this I'm just going to
- 8:45create this down here I'll say new file
- 8:48is equal to and now I'll use this
- 8:51existing path object of this directory
- 8:54and then we have access to this for SL
- 8:58syntax here and then I can just say I'll
- 9:01just call this new file.txt and now
- 9:04let's go ahead and print this out with
- 9:07the others and I'm going to uh take off
- 9:11these stems here so I will grab all of
- 9:16those get rid of those save that and run
- 9:19it and we can see there that our new
- 9:21file is at this path within directory
- 9:24one now if you're used to using the OS
- 9:28module and you're not comfortable
- 9:29jumping directly into using this for SL
- 9:33syntax here for your file pass then you
- 9:35still have access to a method that does
- 9:37the same thing um so instead of using a
- 9:40slash we can use join path as the method
- 9:44so I can say my dur do join path and
- 9:50then we will just pass in that new
- 9:53file.txt and if I save that and run it
- 9:57then we should get the same thing here
- 9:59and we can see that that worked and like
- 10:01I was saying before uh these don't
- 10:04actually need to exist in order to get
- 10:06this information so this new file.txt
- 10:09doesn't currently exist on my file
- 10:12system but we can treat that path just
- 10:15like any other so if we wanted to see if
- 10:17these paths do exist then we could
- 10:20simply use the exist method so let me go
- 10:23ahead and do that at the end of all of
- 10:26these here I will add the exist
- 10:29method I will save that and run it and
- 10:32we can see that for our directory we get
- 10:35true that it does exist this we get true
- 10:38for this file one.txt because it does
- 10:41exist and this new file.txt we can see
- 10:44that we get false because that file
- 10:46actually doesn't exist on our file
- 10:48system okay so now let's maneuver over
- 10:50to looking more at absolute versus
- 10:53relative paths and this is where I
- 10:56believe that path lib really shines over
- 10:58the OS mod modle so first of all let's
- 11:01get an idea of where we currently are on
- 11:03the file system so first let's take a
- 11:06look at the Parent Directory of each of
- 11:09these paths so to do this I can just uh
- 11:13get the dot parent attribute so I will
- 11:17run parent on each of these and print
- 11:20those out so if I run this we can see
- 11:23that for the first two paths we just get
- 11:25this Dot and that means the current
- 11:28working directory is the parent of that
- 11:31path for our new file within directory
- 11:34one we can see that directory one is the
- 11:37parent of that file path and you can
- 11:39chain these together too so if I wanted
- 11:41to go up two levels with this new file
- 11:45then I could say new file.
- 11:47parent. parent if I save that and run it
- 11:51then we can see that now uh it goes up
- 11:54to this current working directory level
- 11:56as well and this is actually as far as
- 11:59the these will go since these are
- 12:00relative paths but maybe we wanted to
- 12:03know exactly where we are on the file
- 12:05system and have an absolute path to
- 12:08these locations so in order to do that
- 12:10we can get an absolute path and there
- 12:13are two ways that we can do this the
- 12:15first way is to use the absolute method
- 12:17and this parent attribute is returning
- 12:21the path object also so we can either
- 12:24use an absolute the absolute method on
- 12:27that parent object uh or on the original
- 12:30object it just depends on how we would
- 12:32want to do this so for example uh what I
- 12:34mean by this is I could either say uh my
- 12:38directory.
- 12:40parent. absolute and if I run that then
- 12:44you can see that now we are getting the
- 12:47absolute path to this path location or I
- 12:51could just run absolute on that original
- 12:56path lib object my dur absolute. parent
- 13:01if I save that and run it then you can
- 13:03see that we get the same thing now if I
- 13:05just wanted to get the absolute path of
- 13:07this directory and not the parent then I
- 13:10could just take that parent off there
- 13:12rerun that now we can see that we get
- 13:14the absolute path to that directory now
- 13:17another way that we can get the absolute
- 13:19path is to use the resolve method and
- 13:22this is the one that I used basically
- 13:2399% of the time uh what this does is it
- 13:27gives you an absolute path but it also
- 13:30resolves any Sim links or relative
- 13:32directory references uh so for example
- 13:35if I change this to resolve so let me go
- 13:39ahead and do that and I will save that
- 13:43and run it and we shouldn't see any
- 13:45differences right now and we don't it
- 13:47just looks like the same absolute path
- 13:50but if there were any Sim links or
- 13:52relative directory references in here
- 13:54then we would see a difference uh so for
- 13:57example let me comment out
- 13:59what we have here and let me create a
- 14:04new path to print out here so I'll say p
- 14:07is equal to path and I will make this
- 14:11path dot dot and then I will do uh dot
- 14:16absolute at the end of that and then
- 14:19let's just print out that absolute path
- 14:22so if I save that and run it then we can
- 14:25see we're getting this weird thing where
- 14:27it's taking us to the directory of our
- 14:29tutorial here and then it has this dot
- 14:32dot directory and that's not really what
- 14:34we want so this dot dot here is a common
- 14:38uh relative directory reference uh that
- 14:41usually means The Parent Directory but
- 14:44using this absolute method isn't
- 14:46resolving what we uh mean here by using
- 14:49that Parent Directory so what we need to
- 14:52do is instead use resolve so I'm going
- 14:55to change this absolute to resolve I
- 14:59will save that and run it and now we can
- 15:02see here that it knows that what we
- 15:04wanted here was our Parent Directory and
- 15:08since we were already within uh this
- 15:10tutorial directory on my desktop then it
- 15:13knows that the Parent Directory that I'm
- 15:15after is my desktop itself so I found
- 15:18that for most of the time for what I
- 15:20need I almost always want to resolve
- 15:23paths when getting their absolute path
- 15:26and if we recall this is basically what
- 15:28we saw in the beginning where we looked
- 15:30at how D jeno changed their code base to
- 15:33using pathlib instead of the OS module
- 15:35so what they were doing was using the uh
- 15:39double uncore file variable which is a
- 15:42special variable that contains the path
- 15:44to the current file and then they were
- 15:46getting the absolute path to that file
- 15:49and they were using the resolve meth
- 15:51method as well so I could do that here
- 15:54uh by saying instead of uh dot dot I
- 15:58will use this file variable and I don't
- 16:01want to put this within quotes because
- 16:03this is actually a special variable here
- 16:05so if I save this and run it then we can
- 16:08see that we are getting the absolute
- 16:10path uh to this specific python script
- 16:13so if you ever want to get the Parent
- 16:15Directory to a specific file like they
- 16:17were doing in those Django setting files
- 16:20uh then hopefully now you understand
- 16:21this a little bit better of what they
- 16:23were doing they were taking the current
- 16:26file they were resolving that absolute
- 16:28abute path and getting that absolute
- 16:30path and then they were just getting uh
- 16:33the parent of that absolute path and in
- 16:37this instance this is going to be the
- 16:39directory that contains my tutorial. py
- 16:42file that we are currently in now in
- 16:45their example uh their project directory
- 16:48was actually two levels up so they put
- 16:49another parent there uh but the concept
- 16:52is the exact same now there are a couple
- 16:54more caveats about uh getting an
- 16:56absolute path that sometimes resolve
- 16:59won't take care of either uh so for
- 17:02example let's say that we wanted to get
- 17:03access to a user's home folder uh so for
- 17:06example I keep my DOT files and a lot of
- 17:09system settings in my DOT files folder
- 17:12of my home directory so a lot of us may
- 17:15be uh used to using the Tilda to access
- 17:18a user's home folder uh so let me uh try
- 17:22that with a path so I'm going to remove
- 17:25this parent for now and now let's say
- 17:28that I wanted to make this path to my
- 17:30home folder and I'll do a Tilda sl.
- 17:34files and this is going to be used as my
- 17:38uh users home folder here now if I save
- 17:41this and I run it then we can see that
- 17:44even with resolve that doesn't work like
- 17:46we'd expect it just added this on to the
- 17:49end of our current working directory so
- 17:52instead if we are parsing a string with
- 17:55the Tilda then you can use the expand
- 17:58and user method to get the correct path
- 18:01so instead of resolve here I will say do
- 18:05expand user and if I save that and run
- 18:10it then we can see that that works it
- 18:12goes to uh users corms which is my home
- 18:15folder uh and then goes into this files
- 18:18directory and just like before if we
- 18:21wanted to then we could chain the
- 18:23resolve method onto the end of this uh
- 18:25if there were any Sim links or relative
- 18:28references in that path now I'd only use
- 18:31this expand user method if I was parsing
- 18:34an already existing string if I was
- 18:36creating this from scratch then it's
- 18:39much better in my opinion to use the
- 18:41home method so if I wanted a path to
- 18:45this files directory then instead I
- 18:47could just say path
- 18:51doome and then we can use that for slash
- 18:54notation and just go straight to the
- 18:57files directory there if I save that and
- 19:00run it then you can see that that gives
- 19:02us the same thing so since I have this
- 19:05home directory here uh let's say that we
- 19:07wanted to search this dot files
- 19:09directory so we saw earlier how we can
- 19:12use the iter dur method to list all the
- 19:14files and folders in our path but it's
- 19:17pretty Limited in terms of searching and
- 19:19also you need to do some extra work in
- 19:22order to get that information from
- 19:23within the subdirectories of our path so
- 19:26in order to do some more advanced
- 19:28searching we can use the glob method so
- 19:31let's say that I wanted to search my do
- 19:33files path for any file or folder that
- 19:37has a VSS code in the name to do this we
- 19:41can use some wild cards with the glob
- 19:43method so really quick let me clean up
- 19:46uh what we have here so far um up here
- 19:49you can see that is where we uh looped
- 19:51over that itter dur method from before
- 19:54but let me go ahead and clean this up
- 19:57and I will actually just call this do
- 20:00files and now let's search this using
- 20:03that glob method so I can say 4 P in.
- 20:09files. glob and again let's say that we
- 20:13wanted to uh find any file or folder
- 20:17that has VSS code in the name so what I
- 20:19can do here is I can use some wild cards
- 20:22and just put vs code here in the middle
- 20:25so it can be at the beginning or it can
- 20:27be at the end or it can be in the middle
- 20:30that's what these wild cards here are so
- 20:32anything at the beginning and anything
- 20:34at the end uh as long as it contains
- 20:37this VSS code so now let's just print
- 20:39out all of those paths uh that match
- 20:43that so I will run that and you can see
- 20:46that we only get one result here for
- 20:48this VH Co VSS code. SH now there could
- 20:52actually be some more matches uh but by
- 20:55default glob only searches the path
- 20:57directory and and not the subdirectories
- 21:00so to search the subdirectories we need
- 21:02to use recursive glob which we can uh
- 21:06use just by adding an R before glob here
- 21:09which stands for recursive glob so oh
- 21:13actually and I almost forgot uh we're
- 21:15still not guaranteed to get all the
- 21:17results that we might be after here
- 21:19because glob is also case sensitive
- 21:22which is sometimes what we want but in
- 21:25this case I don't mind if there is a
- 21:28mixed Cas uh I just want all of the
- 21:30results so I'll add an argument here to
- 21:33this recursive glob and I can say uh
- 21:37that I want case sensitive equal to
- 21:40false so now if I save that and run it
- 21:45then you can see that now we are getting
- 21:46some more results here and these have
- 21:48some mixed cases in them uh and they are
- 21:52also within the subdirectories here and
- 21:55you can use these wild cards in
- 21:57different ways so if I wanted to instead
- 21:59find all of the Json files then I could
- 22:02just use a wild card at the beginning
- 22:05and then say uh anything that has a Json
- 22:10extension I'm not going to put a wild
- 22:12card at the end since a we want a
- 22:15anything that ends in a Json so if I
- 22:18save that and run it then we can see
- 22:20that now we get all of the Json files in
- 22:22those dot files now let's say that we
- 22:24wanted to open one of these files in
- 22:26order to work with them uh so so we can
- 22:29do that just by passing in a path lib
- 22:32object into the open function uh just
- 22:35like you might be used to uh doing with
- 22:37strings so for example if I wanted to
- 22:40open this VSS uh settings. Json file
- 22:44here let me go ahead and just add this
- 22:46to my path so I'm going to copy this
- 22:49settings uh vs code and I will just add
- 22:53this on here at the end and now instead
- 22:58of searching over uh using glob let's go
- 23:01ahead and open this up now to open this
- 23:04file we have a couple of different
- 23:05different options here the path objects
- 23:08actually have an open method of their
- 23:10own so I could say with
- 23:14files. openen and then I can say as F
- 23:20and then let's just
- 23:22print f. read to get the contents of
- 23:25that file and by default that's going to
- 23:28open that in read mode so I can just run
- 23:32this as is and this is my VSS code
- 23:36settings. Json file down here so it did
- 23:40uh get the contents of that file and if
- 23:42we wanted to use the more traditional
- 23:44method of opening files then we can also
- 23:47just pass the pathlib object into the
- 23:50open function instead so instead of
- 23:53saying files. openen I could say open
- 23:57and then
- 23:59pass in dot files so if I save that and
- 24:03run it then you can see that we get the
- 24:05exact same thing here now I'm not going
- 24:07to go in depth on how to read and write
- 24:10files in this video because I have a
- 24:12separate video completely dedicated to
- 24:14that topic that I'll put a link to in
- 24:16the description section below uh so if
- 24:18you are interested then you can give
- 24:20that a watch but the big thing to know
- 24:22here is that we can use these pathlib
- 24:24objects with these open functions
- 24:26directly uh without need to convert them
- 24:29to Strings so that's extremely useful
- 24:32okay so now let's look at a couple more
- 24:34things that we can do with path lib and
- 24:36also we'll see a few examples of where
- 24:38using path lib isn't the best approach
- 24:41and when using some other modules might
- 24:43be better so with pathlib we have some
- 24:45limited ability to create move and
- 24:48delete files and folders so let's take a
- 24:50look at some of these so let's say that
- 24:52I wanted to create a new directory so to
- 24:56do this I'm just going to delete what we
- 24:58have
- 24:58uh so far and I'm going to create a new
- 25:01path here and let's just specify a
- 25:03directory that doesn't exist yet I'll
- 25:05just call this uh temp directory so in
- 25:09order to create this directory I can use
- 25:13that path and then use the make dur
- 25:16method there if I save that and run it
- 25:19if we look over here in our sidebar then
- 25:22we can see that it creates that new temp
- 25:24directory now if this directory had
- 25:27already existed when I I'd run that then
- 25:29I would get an error but there's an
- 25:31argument that I can add to this uh
- 25:34makeer method of I think it's exists
- 25:37okay is equal to true and if I was to
- 25:40specify that then even if that directory
- 25:42already existed then it would not throw
- 25:44an error okay so now let's say that now
- 25:48that we have this directory that I
- 25:49wanted to remove it so to remove this I
- 25:53can just say RM dur on that path if I
- 25:57save that and run it then we can see
- 25:59that now it deleted that directory from
- 26:01our sidebar now one caveat here with
- 26:04removing directories is that this will
- 26:06only remove empty directories if you
- 26:09want to remove a non-empty directory
- 26:11then you'll have to use another module
- 26:14like the Shu till module uh in order to
- 26:17do that now if we wanted to create
- 26:19subdirectories as well then we would
- 26:22have to specify that we wanted to create
- 26:25all the non-existent parent directories
- 26:27along the way so for example if I wanted
- 26:30to create our temp directory plus
- 26:33another subdirectory then let me go
- 26:35ahead and add that in here let's say
- 26:38that we wanted temp directory and this
- 26:41subdirectory here then I could say make
- 26:44dur but if I run this now then it won't
- 26:48work because temp directory doesn't
- 26:51exist what I have to do here is I have
- 26:53to say uh parents is equal to true and
- 26:56that will say go ahead and can create
- 26:58any of the parents that you need along
- 27:01the way so now if I save that and run it
- 27:04then we can see that we have temp
- 27:05directory over here in the sidebar and
- 27:08if I click on that then we have
- 27:09subdirectory uh within there as well now
- 27:13like I'd said before uh you can't delete
- 27:15non-existent directories so if you
- 27:17wanted to delete this temp directory
- 27:20then you'd actually need to delete this
- 27:22subdirectory first and then the temp
- 27:24directory uh or what you could do is use
- 27:27the shu module which allows you to
- 27:30delete non-existent directories so now
- 27:33we've seen a little bit of uh how to
- 27:35work with directories uh but we can also
- 27:38create files as well so to do this let
- 27:41me go ahead and create a new file here
- 27:44I'll just call this temp file.txt in
- 27:48order to create um this file path here
- 27:52what I can do is just say path. touch
- 27:56and when I run touch on that object if I
- 27:59run that then we can see that now we
- 28:01have this temp file.txt over there in
- 28:04our sidebar and if that file had already
- 28:06existed before we ran that um touch then
- 28:09it wouldn't overwrite it it would just
- 28:11update its modification time okay so we
- 28:15can also rename a file or a directory
- 28:18using either the rename or the replace
- 28:20method so if I wanted to rename this
- 28:23temp file to be lowercase then what I
- 28:26could do is on the this path object I
- 28:29could do a do rename and it's going to
- 28:34rename this path object here and I will
- 28:37pass in let's say that we just wanted
- 28:39this to be lowercase so I'll do temp
- 28:41file.txt if I run that and over here in
- 28:45the sidebar we can see that this is now
- 28:48a temp file all lowercase now rename can
- 28:51actually be a little bit tricky on some
- 28:54operating systems like mac and Linux it
- 28:57will overwrite existing files on Windows
- 29:01I believe that you'll get an error if
- 29:03the file already exists um I don't like
- 29:06to leave things like that up the chance
- 29:08so what I usually do if I want to be
- 29:11careful is I can always use a
- 29:13conditional to check if the file already
- 29:15exists uh like we did earlier in the
- 29:18video with that exist method now if you
- 29:21wanted to be more explicit that the file
- 29:24should absolutely be overwritten uh
- 29:26regardless if there's a file or
- 29:28directory already there then you can use
- 29:31the replace method instead of rename and
- 29:35that should work the same on any
- 29:37operating system so I usually use
- 29:39replace instead of rename uh since I do
- 29:42those conditional existence checks
- 29:44myself uh I don't like to rely on a
- 29:47method to do that for me so if I wanted
- 29:50to use replace here then all I would
- 29:52need to do uh since this is named temp
- 29:55file lowercase now let me change our
- 29:57existing path to be lowercase there
- 29:59let's say I wanted to uh replace this
- 30:03with tempcore file instead of rename
- 30:06here I can just say replace and if I
- 30:09save that and run it then we can say
- 30:13that uh replace works as well over here
- 30:15in the sidebar we have _ file.txt and if
- 30:19you don't have any checks in place then
- 30:21that will definitely replace that file
- 30:23or folder uh no matter the operating
- 30:25system so be careful with some of the
- 30:28commands because some of them will
- 30:30overwrite existing data okay so uh
- 30:33lastly if we wanted to remove a file
- 30:36then we can use the unlink method so if
- 30:39I wanted to delete this temp file.txt
- 30:42again let me replace our existing path
- 30:45object up here with what we rename that
- 30:48to so if I wanted to delete that temp
- 30:50file then I could use unlink so if I
- 30:54save that and run it then we can see
- 30:58that that file was deleted from our
- 30:59sidebar here so it is deleted from the
- 31:02file system okay so we're just about
- 31:04finished up here uh I still want to go
- 31:06over some scenarios where we wouldn't
- 31:09want to use pathlib and instead use Os
- 31:12or another module like sh util but first
- 31:16I'd like to thank the sponsor of this
- 31:18video and that's extra wallets so I've
- 31:21actually been using an extra wallet as
- 31:23my daily wallet for about 8 months now
- 31:25and I can tell you that I've really
- 31:26enjoyed this wallet as as a replacement
- 31:28from what I had before the one I
- 31:30specifically have is leather but they
- 31:32come in a lot of different styles and
- 31:34materials their newest pro model comes
- 31:36in 100% aluminum and all of their
- 31:39wallets allow you to access your cards
- 31:41super easily just by pushing a button
- 31:43one of the things I've found most useful
- 31:45is their finder card add-on which is a
- 31:47super slim uh credit card size tracking
- 31:50device that's easily rechargeable and
- 31:52one charge can last as long as uh 6
- 31:55months they have ADD ones that also
- 31:57allow you to add Apple Air tags to their
- 31:59wallets but I find the slim design of
- 32:02their tracking card much better since I
- 32:03don't even notice it in my wallet I'll
- 32:06leave a link to their page in the
- 32:07description section below and if you use
- 32:09code schaer at checkout then you'll
- 32:11unlock some extra savings on top of any
- 32:13current uh promotions that they're
- 32:15already running I'd love it if you all
- 32:17could go show some love to our sponsors
- 32:19and again I'd like to thank extra
- 32:20wallets for sponsoring this
- 32:23video Okay so we've seen why we'd use
- 32:25pathlib instead of the OS module and how
- 32:28it can be more readable and useful in a
- 32:30lot of different scenarios but when
- 32:33should we still use the OS module or
- 32:36another module like the sh util module
- 32:39that we've mentioned a few times before
- 32:41uh well there's a few scenarios so like
- 32:44the name hints uh path lib should be
- 32:47used for paths so when you're actually
- 32:50doing things on the operating system
- 32:52like getting environment variables uh
- 32:55the OS module is still what you want uh
- 32:58to use for things like that and like I
- 33:01mentioned previously pathlib might not
- 33:04uh offer some more advanced features uh
- 33:06that you're looking for like removing
- 33:08non-empty directories or copying files
- 33:12uh so for that you'd want to use
- 33:14something like the shutil module um I've
- 33:17not done a tutorial on the shutil module
- 33:20yet but I do plan on putting together
- 33:22something on that in the near future uh
- 33:25so we can see that what that's actually
- 33:26capable of but for the most part I've
- 33:29switched over to using pathlib and all
- 33:30of my newer projects uh for working with
- 33:33file paths and personally I think that
- 33:35you should too uh since a lot of the
- 33:37larger code bases have made that switch
- 33:39also it might feel a bit strange once
- 33:41you first start using it but once you
- 33:43get used to it it definitely feels more
- 33:45intuitive and easier to work with uh
- 33:47than the OS module so I definitely think
- 33:50that you should give it a try uh but
- 33:51with that said I think that's going to
- 33:53do it for this video hopefully now you
- 33:55have a pretty good understanding of the
- 33:56path lib module and why we want to start
- 33:58using this in our code uh in some
- 34:00upcoming videos I'm going to be getting
- 34:02back to some web development Frameworks
- 34:05and releasing a new series on flask and
- 34:07Django so be sure you're subscribed to
- 34:09be notified about those but if anyone
- 34:11has any questions about what we covered
- 34:13in this video then feel free to ask in
- 34:15the comment section below and I'll do my
- 34:17best to answer those and if you enjoy
- 34:18these tutorials and would like to
- 34:20support them then there are several ways
- 34:21you can do that the easiest ways is
- 34:23simply like the video and give it a
- 34:24thumbs up also it's a huge help to share
- 34:26these videos with any one who you think
- 34:28would find them useful and if you have
- 34:29the means you can contribute through
- 34:31patreon or YouTube and there are links
- 34:33to those pages in the description
- 34:34section below be sure to subscribe for
- 34:36future videos and thank you all for
- 34:37watching
About this transcript
This page contains the full transcript of Python Tutorial: Pathlib - The Modern Way to Handle File Paths by Corey Schafer, generated from the public captions YouTube serves with the video. The transcript has 6,063 words across 781 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.