YouTube2Text

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

by Corey Schafer · 6,063 words · 781 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hey there how's it going everybody in
  2. 0:01this video we're going to be learning
  3. 0:03about the path lib module and how to use
  4. 0:05it now this is used in place of the OS
  5. 0:08module for working with paths and many
  6. 0:10people have been slow uh to move over to
  7. 0:13using this but we're going to be
  8. 0:14learning about the advantages that it
  9. 0:17provides over using the OS module and
  10. 0:19why we should probably make that switch
  11. 0:21now we do have a sponsor for this video
  12. 0:23which is extra wallets so I'd like to
  13. 0:25thank them for sponsoring the video and
  14. 0:27we'll talk more about what they offer
  15. 0:29after this lesson but for now let's take
  16. 0:31a look at the path lib module and why
  17. 0:33it's now the preferred way to work with
  18. 0:36paths in Python so path lib was
  19. 0:38introduced to the standard library in
  20. 0:40Python 3.4 and provides an easy way to
  21. 0:43work with file paths and the file system
  22. 0:46so what's the big difference between
  23. 0:48pathlib and the OS module well the OS
  24. 0:50module uh you're working with strings
  25. 0:53for paths which can get a bit messy
  26. 0:56especially when working between
  27. 0:57different operating systems but with
  28. 0:59path lib it represents file paths as
  29. 1:02objects rather than strings and this
  30. 1:04will uh better handle those problems a
  31. 1:06bit more and it's more intuitive uh
  32. 1:09readable and less prone to errors and
  33. 1:12I'm not just bringing this up as a cool
  34. 1:14python tip or trick uh there are some
  35. 1:17large code bases out there that have
  36. 1:18switched over to using pathlib uh so for
  37. 1:21example in the script that I have open
  38. 1:23here I have two examples of a line from
  39. 1:26a a Jango projects settings file the
  40. 1:30first one is from an older d jeno
  41. 1:32project where they were still using the
  42. 1:34OS module and the second one down here
  43. 1:37is from a newer jeno project where
  44. 1:39they've switched over to using pathlib
  45. 1:42and we can see the differences here if
  46. 1:44we were to look at the first example
  47. 1:46using the OS module it's kind of hard to
  48. 1:48tell what's going on you have to break
  49. 1:51it down slowly and think okay so we're
  50. 1:54getting the absolute uh file path of
  51. 1:57this double underscore file and then
  52. 2:00we're getting the uh directory of that
  53. 2:04absolute path and then we're getting the
  54. 2:06directory of that directory and it's a
  55. 2:09bit confusing when we try to break it
  56. 2:10down like that but basically all they're
  57. 2:13trying to do is set the base directory
  58. 2:16to the main project directory which is
  59. 2:18two levels up from the settings file now
  60. 2:22if we compare that to uh path lib down
  61. 2:25here then all we are doing is resolving
  62. 2:28the absolute path of this uh file
  63. 2:32variable here and then we're getting the
  64. 2:34parent of that and then the parent of
  65. 2:36that and if any of this is confusing
  66. 2:38don't worry we're going to go over all
  67. 2:39this in the video but I just wanted to
  68. 2:41show you the differences uh in kind of
  69. 2:44how you break things down here we can
  70. 2:46see that this OS one is a lot longer and
  71. 2:49we kind of have to read this from right
  72. 2:51to left where with pathlib we're uh this
  73. 2:54is a lot shorter we're reading this from
  74. 2:56left to right and it's just more
  75. 2:59intuitive as to what's going on and we
  76. 3:01can see here that these comments with
  77. 3:03these Django files are different as well
  78. 3:06we can see that to build the paths
  79. 3:08inside a project that they want you to
  80. 3:10use os. path. jooin with this base
  81. 3:13directory um in this older example and
  82. 3:17in the newer example they just have this
  83. 3:19base directory for SLS subdirectory and
  84. 3:22again we're going to take a look at all
  85. 3:24this in the video I just wanted to show
  86. 3:26you some real world examples right off
  87. 3:28the bat of some larger code bases that
  88. 3:30have made this switch so now let's go
  89. 3:32ahead and jump into learning how to use
  90. 3:34pathlib so I'm going to go ahead and
  91. 3:37delete those examples that we have and
  92. 3:39now you can see that all we're importing
  93. 3:41here is the OS module and we're also
  94. 3:43importing path from path lib so we'll
  95. 3:47walk through using pathlib for some
  96. 3:48different things and we'll also see
  97. 3:51examples of when to use pathlib or when
  98. 3:53to use um the OS module or another
  99. 3:57module if it has a better use case so
  100. 3:59first of all let's just look at the
  101. 4:01current directory that we are currently
  102. 4:03in to do this I'm just going to print
  103. 4:06out path and that is CWD for current
  104. 4:11working directory so I'm going to save
  105. 4:13that and run it and then get my output
  106. 4:16open here and we can see that we're
  107. 4:18currently in this tutorial directory on
  108. 4:21my desktop now let's list the files and
  109. 4:24folders within this current tutorial
  110. 4:27directory so if we use path without any
  111. 4:31arguments then it will default to the
  112. 4:33current working directory which is uh
  113. 4:36this right here what we just printed out
  114. 4:38so if we wanted to iterate over all of
  115. 4:41the files and folders in this current
  116. 4:44directory then we could simply say 4 p
  117. 4:49in path and I won't pass in any
  118. 4:52arguments and by default that'll just be
  119. 4:54the current working directory and we can
  120. 4:56use iter dur and this will make an
  121. 5:00iterator of all the files and folders in
  122. 5:04that current directory so I will just
  123. 5:06print all those out go ahead and run
  124. 5:08that and we can see that it shows us all
  125. 5:10the files and folders within our current
  126. 5:12working directory so we have a file.txt
  127. 5:15uh tutorial. py which is the python file
  128. 5:18that I'm running here and then this
  129. 5:20directory one and we can also see these
  130. 5:23over here in the sidebar so let me go
  131. 5:25ahead and take both this file one and
  132. 5:28this directory one and create specific
  133. 5:31path objects for each of those uh so
  134. 5:33since they're in my current directory I
  135. 5:36can uh simply say I'll comment this out
  136. 5:39for now I can just say uh my dur is
  137. 5:43equal to and A New Path object and we
  138. 5:47will just pass in directory one here and
  139. 5:52now I'll also do this for file so I'll
  140. 5:55do file one.txt and I will rename this
  141. 5:59variable to to my file so the way that
  142. 6:02I've done this so far is by using a
  143. 6:04relative path since I'm only typing in
  144. 6:07the directory name and the file name
  145. 6:09with nothing else it's looking for those
  146. 6:12relative to my current working directory
  147. 6:15now we can also use absolute paths as
  148. 6:17well and we'll take a look at that in
  149. 6:19just a bit but for now let's look at
  150. 6:22some of the information that we can get
  151. 6:23from these relative path objects so
  152. 6:26first let me print these out so we can
  153. 6:28see uh the output from both of these if
  154. 6:31I just print both of these out I will
  155. 6:35print out my directory and I will also
  156. 6:39print out my file and right now if I
  157. 6:43save this and run it then we can see
  158. 6:45that it gives the name of both of these
  159. 6:48now if I wanted to be explicit then I
  160. 6:51could go ahead and print out uh the name
  161. 6:55attribute for both of these so I'll go
  162. 6:57ahead and do that if I save that and run
  163. 6:59it then we can see that we get the same
  164. 7:01thing here uh we are getting the name
  165. 7:03for directory for our directory and our
  166. 7:05name for our file now another common
  167. 7:08thing that we might want to do is get
  168. 7:09the file extension now if you're using
  169. 7:12the OS module you might remember that to
  170. 7:14do this with that module you would first
  171. 7:16need to split the file name and the
  172. 7:19extension and then grab the index of
  173. 7:21whichever one you'd like uh but with
  174. 7:24pathlib this is just as easy as getting
  175. 7:27the suffix attribute so I can say suffix
  176. 7:31and I'll do that for both of these here
  177. 7:34so if I save that and run it then we can
  178. 7:37see that for our directory we don't have
  179. 7:39an extension since that is a directory
  180. 7:41and for our text file we have this txt
  181. 7:44extension and to get the file name
  182. 7:46without the extension that is what's
  183. 7:49called the stem so to do that I'll go
  184. 7:52ahead and replace both of those with
  185. 7:56stem if I save that and run it then we
  186. 7:59can see that now we just get directory
  187. 8:01one and it didn't have an extension to
  188. 8:03begin with and now for our file.txt
  189. 8:05we're just getting file one without that
  190. 8:08txt extension okay so now let's say that
  191. 8:11we wanted to access a file within this
  192. 8:14directory one here now these paths don't
  193. 8:17actually need to already exist when we
  194. 8:19create these path objects so for example
  195. 8:22directory one is currently empty so
  196. 8:25let's create a path to a new text file
  197. 8:27within that directory so with the OS
  198. 8:30module the way that we would do this was
  199. 8:32with os. path. jooin but with pathlib we
  200. 8:37have the ability to use this for SL
  201. 8:39operator that's common on a lot of file
  202. 8:42systems so to do this I'm just going to
  203. 8:45create this down here I'll say new file
  204. 8:48is equal to and now I'll use this
  205. 8:51existing path object of this directory
  206. 8:54and then we have access to this for SL
  207. 8:58syntax here and then I can just say I'll
  208. 9:01just call this new file.txt and now
  209. 9:04let's go ahead and print this out with
  210. 9:07the others and I'm going to uh take off
  211. 9:11these stems here so I will grab all of
  212. 9:16those get rid of those save that and run
  213. 9:19it and we can see there that our new
  214. 9:21file is at this path within directory
  215. 9:24one now if you're used to using the OS
  216. 9:28module and you're not comfortable
  217. 9:29jumping directly into using this for SL
  218. 9:33syntax here for your file pass then you
  219. 9:35still have access to a method that does
  220. 9:37the same thing um so instead of using a
  221. 9:40slash we can use join path as the method
  222. 9:44so I can say my dur do join path and
  223. 9:50then we will just pass in that new
  224. 9:53file.txt and if I save that and run it
  225. 9:57then we should get the same thing here
  226. 9:59and we can see that that worked and like
  227. 10:01I was saying before uh these don't
  228. 10:04actually need to exist in order to get
  229. 10:06this information so this new file.txt
  230. 10:09doesn't currently exist on my file
  231. 10:12system but we can treat that path just
  232. 10:15like any other so if we wanted to see if
  233. 10:17these paths do exist then we could
  234. 10:20simply use the exist method so let me go
  235. 10:23ahead and do that at the end of all of
  236. 10:26these here I will add the exist
  237. 10:29method I will save that and run it and
  238. 10:32we can see that for our directory we get
  239. 10:35true that it does exist this we get true
  240. 10:38for this file one.txt because it does
  241. 10:41exist and this new file.txt we can see
  242. 10:44that we get false because that file
  243. 10:46actually doesn't exist on our file
  244. 10:48system okay so now let's maneuver over
  245. 10:50to looking more at absolute versus
  246. 10:53relative paths and this is where I
  247. 10:56believe that path lib really shines over
  248. 10:58the OS mod modle so first of all let's
  249. 11:01get an idea of where we currently are on
  250. 11:03the file system so first let's take a
  251. 11:06look at the Parent Directory of each of
  252. 11:09these paths so to do this I can just uh
  253. 11:13get the dot parent attribute so I will
  254. 11:17run parent on each of these and print
  255. 11:20those out so if I run this we can see
  256. 11:23that for the first two paths we just get
  257. 11:25this Dot and that means the current
  258. 11:28working directory is the parent of that
  259. 11:31path for our new file within directory
  260. 11:34one we can see that directory one is the
  261. 11:37parent of that file path and you can
  262. 11:39chain these together too so if I wanted
  263. 11:41to go up two levels with this new file
  264. 11:45then I could say new file.
  265. 11:47parent. parent if I save that and run it
  266. 11:51then we can see that now uh it goes up
  267. 11:54to this current working directory level
  268. 11:56as well and this is actually as far as
  269. 11:59the these will go since these are
  270. 12:00relative paths but maybe we wanted to
  271. 12:03know exactly where we are on the file
  272. 12:05system and have an absolute path to
  273. 12:08these locations so in order to do that
  274. 12:10we can get an absolute path and there
  275. 12:13are two ways that we can do this the
  276. 12:15first way is to use the absolute method
  277. 12:17and this parent attribute is returning
  278. 12:21the path object also so we can either
  279. 12:24use an absolute the absolute method on
  280. 12:27that parent object uh or on the original
  281. 12:30object it just depends on how we would
  282. 12:32want to do this so for example uh what I
  283. 12:34mean by this is I could either say uh my
  284. 12:38directory.
  285. 12:40parent. absolute and if I run that then
  286. 12:44you can see that now we are getting the
  287. 12:47absolute path to this path location or I
  288. 12:51could just run absolute on that original
  289. 12:56path lib object my dur absolute. parent
  290. 13:01if I save that and run it then you can
  291. 13:03see that we get the same thing now if I
  292. 13:05just wanted to get the absolute path of
  293. 13:07this directory and not the parent then I
  294. 13:10could just take that parent off there
  295. 13:12rerun that now we can see that we get
  296. 13:14the absolute path to that directory now
  297. 13:17another way that we can get the absolute
  298. 13:19path is to use the resolve method and
  299. 13:22this is the one that I used basically
  300. 13:2399% of the time uh what this does is it
  301. 13:27gives you an absolute path but it also
  302. 13:30resolves any Sim links or relative
  303. 13:32directory references uh so for example
  304. 13:35if I change this to resolve so let me go
  305. 13:39ahead and do that and I will save that
  306. 13:43and run it and we shouldn't see any
  307. 13:45differences right now and we don't it
  308. 13:47just looks like the same absolute path
  309. 13:50but if there were any Sim links or
  310. 13:52relative directory references in here
  311. 13:54then we would see a difference uh so for
  312. 13:57example let me comment out
  313. 13:59what we have here and let me create a
  314. 14:04new path to print out here so I'll say p
  315. 14:07is equal to path and I will make this
  316. 14:11path dot dot and then I will do uh dot
  317. 14:16absolute at the end of that and then
  318. 14:19let's just print out that absolute path
  319. 14:22so if I save that and run it then we can
  320. 14:25see we're getting this weird thing where
  321. 14:27it's taking us to the directory of our
  322. 14:29tutorial here and then it has this dot
  323. 14:32dot directory and that's not really what
  324. 14:34we want so this dot dot here is a common
  325. 14:38uh relative directory reference uh that
  326. 14:41usually means The Parent Directory but
  327. 14:44using this absolute method isn't
  328. 14:46resolving what we uh mean here by using
  329. 14:49that Parent Directory so what we need to
  330. 14:52do is instead use resolve so I'm going
  331. 14:55to change this absolute to resolve I
  332. 14:59will save that and run it and now we can
  333. 15:02see here that it knows that what we
  334. 15:04wanted here was our Parent Directory and
  335. 15:08since we were already within uh this
  336. 15:10tutorial directory on my desktop then it
  337. 15:13knows that the Parent Directory that I'm
  338. 15:15after is my desktop itself so I found
  339. 15:18that for most of the time for what I
  340. 15:20need I almost always want to resolve
  341. 15:23paths when getting their absolute path
  342. 15:26and if we recall this is basically what
  343. 15:28we saw in the beginning where we looked
  344. 15:30at how D jeno changed their code base to
  345. 15:33using pathlib instead of the OS module
  346. 15:35so what they were doing was using the uh
  347. 15:39double uncore file variable which is a
  348. 15:42special variable that contains the path
  349. 15:44to the current file and then they were
  350. 15:46getting the absolute path to that file
  351. 15:49and they were using the resolve meth
  352. 15:51method as well so I could do that here
  353. 15:54uh by saying instead of uh dot dot I
  354. 15:58will use this file variable and I don't
  355. 16:01want to put this within quotes because
  356. 16:03this is actually a special variable here
  357. 16:05so if I save this and run it then we can
  358. 16:08see that we are getting the absolute
  359. 16:10path uh to this specific python script
  360. 16:13so if you ever want to get the Parent
  361. 16:15Directory to a specific file like they
  362. 16:17were doing in those Django setting files
  363. 16:20uh then hopefully now you understand
  364. 16:21this a little bit better of what they
  365. 16:23were doing they were taking the current
  366. 16:26file they were resolving that absolute
  367. 16:28abute path and getting that absolute
  368. 16:30path and then they were just getting uh
  369. 16:33the parent of that absolute path and in
  370. 16:37this instance this is going to be the
  371. 16:39directory that contains my tutorial. py
  372. 16:42file that we are currently in now in
  373. 16:45their example uh their project directory
  374. 16:48was actually two levels up so they put
  375. 16:49another parent there uh but the concept
  376. 16:52is the exact same now there are a couple
  377. 16:54more caveats about uh getting an
  378. 16:56absolute path that sometimes resolve
  379. 16:59won't take care of either uh so for
  380. 17:02example let's say that we wanted to get
  381. 17:03access to a user's home folder uh so for
  382. 17:06example I keep my DOT files and a lot of
  383. 17:09system settings in my DOT files folder
  384. 17:12of my home directory so a lot of us may
  385. 17:15be uh used to using the Tilda to access
  386. 17:18a user's home folder uh so let me uh try
  387. 17:22that with a path so I'm going to remove
  388. 17:25this parent for now and now let's say
  389. 17:28that I wanted to make this path to my
  390. 17:30home folder and I'll do a Tilda sl.
  391. 17:34files and this is going to be used as my
  392. 17:38uh users home folder here now if I save
  393. 17:41this and I run it then we can see that
  394. 17:44even with resolve that doesn't work like
  395. 17:46we'd expect it just added this on to the
  396. 17:49end of our current working directory so
  397. 17:52instead if we are parsing a string with
  398. 17:55the Tilda then you can use the expand
  399. 17:58and user method to get the correct path
  400. 18:01so instead of resolve here I will say do
  401. 18:05expand user and if I save that and run
  402. 18:10it then we can see that that works it
  403. 18:12goes to uh users corms which is my home
  404. 18:15folder uh and then goes into this files
  405. 18:18directory and just like before if we
  406. 18:21wanted to then we could chain the
  407. 18:23resolve method onto the end of this uh
  408. 18:25if there were any Sim links or relative
  409. 18:28references in that path now I'd only use
  410. 18:31this expand user method if I was parsing
  411. 18:34an already existing string if I was
  412. 18:36creating this from scratch then it's
  413. 18:39much better in my opinion to use the
  414. 18:41home method so if I wanted a path to
  415. 18:45this files directory then instead I
  416. 18:47could just say path
  417. 18:51doome and then we can use that for slash
  418. 18:54notation and just go straight to the
  419. 18:57files directory there if I save that and
  420. 19:00run it then you can see that that gives
  421. 19:02us the same thing so since I have this
  422. 19:05home directory here uh let's say that we
  423. 19:07wanted to search this dot files
  424. 19:09directory so we saw earlier how we can
  425. 19:12use the iter dur method to list all the
  426. 19:14files and folders in our path but it's
  427. 19:17pretty Limited in terms of searching and
  428. 19:19also you need to do some extra work in
  429. 19:22order to get that information from
  430. 19:23within the subdirectories of our path so
  431. 19:26in order to do some more advanced
  432. 19:28searching we can use the glob method so
  433. 19:31let's say that I wanted to search my do
  434. 19:33files path for any file or folder that
  435. 19:37has a VSS code in the name to do this we
  436. 19:41can use some wild cards with the glob
  437. 19:43method so really quick let me clean up
  438. 19:46uh what we have here so far um up here
  439. 19:49you can see that is where we uh looped
  440. 19:51over that itter dur method from before
  441. 19:54but let me go ahead and clean this up
  442. 19:57and I will actually just call this do
  443. 20:00files and now let's search this using
  444. 20:03that glob method so I can say 4 P in.
  445. 20:09files. glob and again let's say that we
  446. 20:13wanted to uh find any file or folder
  447. 20:17that has VSS code in the name so what I
  448. 20:19can do here is I can use some wild cards
  449. 20:22and just put vs code here in the middle
  450. 20:25so it can be at the beginning or it can
  451. 20:27be at the end or it can be in the middle
  452. 20:30that's what these wild cards here are so
  453. 20:32anything at the beginning and anything
  454. 20:34at the end uh as long as it contains
  455. 20:37this VSS code so now let's just print
  456. 20:39out all of those paths uh that match
  457. 20:43that so I will run that and you can see
  458. 20:46that we only get one result here for
  459. 20:48this VH Co VSS code. SH now there could
  460. 20:52actually be some more matches uh but by
  461. 20:55default glob only searches the path
  462. 20:57directory and and not the subdirectories
  463. 21:00so to search the subdirectories we need
  464. 21:02to use recursive glob which we can uh
  465. 21:06use just by adding an R before glob here
  466. 21:09which stands for recursive glob so oh
  467. 21:13actually and I almost forgot uh we're
  468. 21:15still not guaranteed to get all the
  469. 21:17results that we might be after here
  470. 21:19because glob is also case sensitive
  471. 21:22which is sometimes what we want but in
  472. 21:25this case I don't mind if there is a
  473. 21:28mixed Cas uh I just want all of the
  474. 21:30results so I'll add an argument here to
  475. 21:33this recursive glob and I can say uh
  476. 21:37that I want case sensitive equal to
  477. 21:40false so now if I save that and run it
  478. 21:45then you can see that now we are getting
  479. 21:46some more results here and these have
  480. 21:48some mixed cases in them uh and they are
  481. 21:52also within the subdirectories here and
  482. 21:55you can use these wild cards in
  483. 21:57different ways so if I wanted to instead
  484. 21:59find all of the Json files then I could
  485. 22:02just use a wild card at the beginning
  486. 22:05and then say uh anything that has a Json
  487. 22:10extension I'm not going to put a wild
  488. 22:12card at the end since a we want a
  489. 22:15anything that ends in a Json so if I
  490. 22:18save that and run it then we can see
  491. 22:20that now we get all of the Json files in
  492. 22:22those dot files now let's say that we
  493. 22:24wanted to open one of these files in
  494. 22:26order to work with them uh so so we can
  495. 22:29do that just by passing in a path lib
  496. 22:32object into the open function uh just
  497. 22:35like you might be used to uh doing with
  498. 22:37strings so for example if I wanted to
  499. 22:40open this VSS uh settings. Json file
  500. 22:44here let me go ahead and just add this
  501. 22:46to my path so I'm going to copy this
  502. 22:49settings uh vs code and I will just add
  503. 22:53this on here at the end and now instead
  504. 22:58of searching over uh using glob let's go
  505. 23:01ahead and open this up now to open this
  506. 23:04file we have a couple of different
  507. 23:05different options here the path objects
  508. 23:08actually have an open method of their
  509. 23:10own so I could say with
  510. 23:14files. openen and then I can say as F
  511. 23:20and then let's just
  512. 23:22print f. read to get the contents of
  513. 23:25that file and by default that's going to
  514. 23:28open that in read mode so I can just run
  515. 23:32this as is and this is my VSS code
  516. 23:36settings. Json file down here so it did
  517. 23:40uh get the contents of that file and if
  518. 23:42we wanted to use the more traditional
  519. 23:44method of opening files then we can also
  520. 23:47just pass the pathlib object into the
  521. 23:50open function instead so instead of
  522. 23:53saying files. openen I could say open
  523. 23:57and then
  524. 23:59pass in dot files so if I save that and
  525. 24:03run it then you can see that we get the
  526. 24:05exact same thing here now I'm not going
  527. 24:07to go in depth on how to read and write
  528. 24:10files in this video because I have a
  529. 24:12separate video completely dedicated to
  530. 24:14that topic that I'll put a link to in
  531. 24:16the description section below uh so if
  532. 24:18you are interested then you can give
  533. 24:20that a watch but the big thing to know
  534. 24:22here is that we can use these pathlib
  535. 24:24objects with these open functions
  536. 24:26directly uh without need to convert them
  537. 24:29to Strings so that's extremely useful
  538. 24:32okay so now let's look at a couple more
  539. 24:34things that we can do with path lib and
  540. 24:36also we'll see a few examples of where
  541. 24:38using path lib isn't the best approach
  542. 24:41and when using some other modules might
  543. 24:43be better so with pathlib we have some
  544. 24:45limited ability to create move and
  545. 24:48delete files and folders so let's take a
  546. 24:50look at some of these so let's say that
  547. 24:52I wanted to create a new directory so to
  548. 24:56do this I'm just going to delete what we
  549. 24:58have
  550. 24:58uh so far and I'm going to create a new
  551. 25:01path here and let's just specify a
  552. 25:03directory that doesn't exist yet I'll
  553. 25:05just call this uh temp directory so in
  554. 25:09order to create this directory I can use
  555. 25:13that path and then use the make dur
  556. 25:16method there if I save that and run it
  557. 25:19if we look over here in our sidebar then
  558. 25:22we can see that it creates that new temp
  559. 25:24directory now if this directory had
  560. 25:27already existed when I I'd run that then
  561. 25:29I would get an error but there's an
  562. 25:31argument that I can add to this uh
  563. 25:34makeer method of I think it's exists
  564. 25:37okay is equal to true and if I was to
  565. 25:40specify that then even if that directory
  566. 25:42already existed then it would not throw
  567. 25:44an error okay so now let's say that now
  568. 25:48that we have this directory that I
  569. 25:49wanted to remove it so to remove this I
  570. 25:53can just say RM dur on that path if I
  571. 25:57save that and run it then we can see
  572. 25:59that now it deleted that directory from
  573. 26:01our sidebar now one caveat here with
  574. 26:04removing directories is that this will
  575. 26:06only remove empty directories if you
  576. 26:09want to remove a non-empty directory
  577. 26:11then you'll have to use another module
  578. 26:14like the Shu till module uh in order to
  579. 26:17do that now if we wanted to create
  580. 26:19subdirectories as well then we would
  581. 26:22have to specify that we wanted to create
  582. 26:25all the non-existent parent directories
  583. 26:27along the way so for example if I wanted
  584. 26:30to create our temp directory plus
  585. 26:33another subdirectory then let me go
  586. 26:35ahead and add that in here let's say
  587. 26:38that we wanted temp directory and this
  588. 26:41subdirectory here then I could say make
  589. 26:44dur but if I run this now then it won't
  590. 26:48work because temp directory doesn't
  591. 26:51exist what I have to do here is I have
  592. 26:53to say uh parents is equal to true and
  593. 26:56that will say go ahead and can create
  594. 26:58any of the parents that you need along
  595. 27:01the way so now if I save that and run it
  596. 27:04then we can see that we have temp
  597. 27:05directory over here in the sidebar and
  598. 27:08if I click on that then we have
  599. 27:09subdirectory uh within there as well now
  600. 27:13like I'd said before uh you can't delete
  601. 27:15non-existent directories so if you
  602. 27:17wanted to delete this temp directory
  603. 27:20then you'd actually need to delete this
  604. 27:22subdirectory first and then the temp
  605. 27:24directory uh or what you could do is use
  606. 27:27the shu module which allows you to
  607. 27:30delete non-existent directories so now
  608. 27:33we've seen a little bit of uh how to
  609. 27:35work with directories uh but we can also
  610. 27:38create files as well so to do this let
  611. 27:41me go ahead and create a new file here
  612. 27:44I'll just call this temp file.txt in
  613. 27:48order to create um this file path here
  614. 27:52what I can do is just say path. touch
  615. 27:56and when I run touch on that object if I
  616. 27:59run that then we can see that now we
  617. 28:01have this temp file.txt over there in
  618. 28:04our sidebar and if that file had already
  619. 28:06existed before we ran that um touch then
  620. 28:09it wouldn't overwrite it it would just
  621. 28:11update its modification time okay so we
  622. 28:15can also rename a file or a directory
  623. 28:18using either the rename or the replace
  624. 28:20method so if I wanted to rename this
  625. 28:23temp file to be lowercase then what I
  626. 28:26could do is on the this path object I
  627. 28:29could do a do rename and it's going to
  628. 28:34rename this path object here and I will
  629. 28:37pass in let's say that we just wanted
  630. 28:39this to be lowercase so I'll do temp
  631. 28:41file.txt if I run that and over here in
  632. 28:45the sidebar we can see that this is now
  633. 28:48a temp file all lowercase now rename can
  634. 28:51actually be a little bit tricky on some
  635. 28:54operating systems like mac and Linux it
  636. 28:57will overwrite existing files on Windows
  637. 29:01I believe that you'll get an error if
  638. 29:03the file already exists um I don't like
  639. 29:06to leave things like that up the chance
  640. 29:08so what I usually do if I want to be
  641. 29:11careful is I can always use a
  642. 29:13conditional to check if the file already
  643. 29:15exists uh like we did earlier in the
  644. 29:18video with that exist method now if you
  645. 29:21wanted to be more explicit that the file
  646. 29:24should absolutely be overwritten uh
  647. 29:26regardless if there's a file or
  648. 29:28directory already there then you can use
  649. 29:31the replace method instead of rename and
  650. 29:35that should work the same on any
  651. 29:37operating system so I usually use
  652. 29:39replace instead of rename uh since I do
  653. 29:42those conditional existence checks
  654. 29:44myself uh I don't like to rely on a
  655. 29:47method to do that for me so if I wanted
  656. 29:50to use replace here then all I would
  657. 29:52need to do uh since this is named temp
  658. 29:55file lowercase now let me change our
  659. 29:57existing path to be lowercase there
  660. 29:59let's say I wanted to uh replace this
  661. 30:03with tempcore file instead of rename
  662. 30:06here I can just say replace and if I
  663. 30:09save that and run it then we can say
  664. 30:13that uh replace works as well over here
  665. 30:15in the sidebar we have _ file.txt and if
  666. 30:19you don't have any checks in place then
  667. 30:21that will definitely replace that file
  668. 30:23or folder uh no matter the operating
  669. 30:25system so be careful with some of the
  670. 30:28commands because some of them will
  671. 30:30overwrite existing data okay so uh
  672. 30:33lastly if we wanted to remove a file
  673. 30:36then we can use the unlink method so if
  674. 30:39I wanted to delete this temp file.txt
  675. 30:42again let me replace our existing path
  676. 30:45object up here with what we rename that
  677. 30:48to so if I wanted to delete that temp
  678. 30:50file then I could use unlink so if I
  679. 30:54save that and run it then we can see
  680. 30:58that that file was deleted from our
  681. 30:59sidebar here so it is deleted from the
  682. 31:02file system okay so we're just about
  683. 31:04finished up here uh I still want to go
  684. 31:06over some scenarios where we wouldn't
  685. 31:09want to use pathlib and instead use Os
  686. 31:12or another module like sh util but first
  687. 31:16I'd like to thank the sponsor of this
  688. 31:18video and that's extra wallets so I've
  689. 31:21actually been using an extra wallet as
  690. 31:23my daily wallet for about 8 months now
  691. 31:25and I can tell you that I've really
  692. 31:26enjoyed this wallet as as a replacement
  693. 31:28from what I had before the one I
  694. 31:30specifically have is leather but they
  695. 31:32come in a lot of different styles and
  696. 31:34materials their newest pro model comes
  697. 31:36in 100% aluminum and all of their
  698. 31:39wallets allow you to access your cards
  699. 31:41super easily just by pushing a button
  700. 31:43one of the things I've found most useful
  701. 31:45is their finder card add-on which is a
  702. 31:47super slim uh credit card size tracking
  703. 31:50device that's easily rechargeable and
  704. 31:52one charge can last as long as uh 6
  705. 31:55months they have ADD ones that also
  706. 31:57allow you to add Apple Air tags to their
  707. 31:59wallets but I find the slim design of
  708. 32:02their tracking card much better since I
  709. 32:03don't even notice it in my wallet I'll
  710. 32:06leave a link to their page in the
  711. 32:07description section below and if you use
  712. 32:09code schaer at checkout then you'll
  713. 32:11unlock some extra savings on top of any
  714. 32:13current uh promotions that they're
  715. 32:15already running I'd love it if you all
  716. 32:17could go show some love to our sponsors
  717. 32:19and again I'd like to thank extra
  718. 32:20wallets for sponsoring this
  719. 32:23video Okay so we've seen why we'd use
  720. 32:25pathlib instead of the OS module and how
  721. 32:28it can be more readable and useful in a
  722. 32:30lot of different scenarios but when
  723. 32:33should we still use the OS module or
  724. 32:36another module like the sh util module
  725. 32:39that we've mentioned a few times before
  726. 32:41uh well there's a few scenarios so like
  727. 32:44the name hints uh path lib should be
  728. 32:47used for paths so when you're actually
  729. 32:50doing things on the operating system
  730. 32:52like getting environment variables uh
  731. 32:55the OS module is still what you want uh
  732. 32:58to use for things like that and like I
  733. 33:01mentioned previously pathlib might not
  734. 33:04uh offer some more advanced features uh
  735. 33:06that you're looking for like removing
  736. 33:08non-empty directories or copying files
  737. 33:12uh so for that you'd want to use
  738. 33:14something like the shutil module um I've
  739. 33:17not done a tutorial on the shutil module
  740. 33:20yet but I do plan on putting together
  741. 33:22something on that in the near future uh
  742. 33:25so we can see that what that's actually
  743. 33:26capable of but for the most part I've
  744. 33:29switched over to using pathlib and all
  745. 33:30of my newer projects uh for working with
  746. 33:33file paths and personally I think that
  747. 33:35you should too uh since a lot of the
  748. 33:37larger code bases have made that switch
  749. 33:39also it might feel a bit strange once
  750. 33:41you first start using it but once you
  751. 33:43get used to it it definitely feels more
  752. 33:45intuitive and easier to work with uh
  753. 33:47than the OS module so I definitely think
  754. 33:50that you should give it a try uh but
  755. 33:51with that said I think that's going to
  756. 33:53do it for this video hopefully now you
  757. 33:55have a pretty good understanding of the
  758. 33:56path lib module and why we want to start
  759. 33:58using this in our code uh in some
  760. 34:00upcoming videos I'm going to be getting
  761. 34:02back to some web development Frameworks
  762. 34:05and releasing a new series on flask and
  763. 34:07Django so be sure you're subscribed to
  764. 34:09be notified about those but if anyone
  765. 34:11has any questions about what we covered
  766. 34:13in this video then feel free to ask in
  767. 34:15the comment section below and I'll do my
  768. 34:17best to answer those and if you enjoy
  769. 34:18these tutorials and would like to
  770. 34:20support them then there are several ways
  771. 34:21you can do that the easiest ways is
  772. 34:23simply like the video and give it a
  773. 34:24thumbs up also it's a huge help to share
  774. 34:26these videos with any one who you think
  775. 34:28would find them useful and if you have
  776. 34:29the means you can contribute through
  777. 34:31patreon or YouTube and there are links
  778. 34:33to those pages in the description
  779. 34:34section below be sure to subscribe for
  780. 34:36future videos and thank you all for
  781. 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.