A Deep Dive Into Pathlib And The Magic Behind It

preview_player
Показать описание
If you’re not yet using pathlib for dealing with files and directories, you’re missing out. This video takes a close look at the pathlib library in Python and explains some of the magic that goes into it, as well as how you can use it in your own code.

🎓 Courses:

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- James Dooley
- Dale Hagglund

🔖 Chapters:
0:00 Intro
0:55 Why not simply use strings
1:10 Issues with strings
2:20 Basic usage of pathlib
4:02 Back slashes and forward slashes
5:13 Reading file from a path
6:03 Resolving paths
6:41 Useful path properties
8:15 Checking whether path is a file or a directory
8:43 Creating and deleting files and directories
10:43 Reading paths from configuration files
11:13 Pathlib and Pydantic
12:31 Operator overloading in Python
16:34 Outro

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

Functions that accept Path are quite a mess without type annotations, because a lot of users intuitively try to call them with strings. Having a mix of functions that expect Path or str in your code is inevitable due to external libraries. For more public functions I often accept union and run `path = Path(path)` in the very first line. I don't think we'll be ever able to get rid of this assumption that Path and str can be used interchangeably, but that's fine as long as everyone is aware of that. It's almost like a natural language - you can't force it.

pawelkubik
Автор

Again, a great example of properly paced, informative, and reliable tips and insights. I stumbled over several chapters of a textbook and multiple web sources before searching diligently for Argan options on this library. As a retired hobbyist, I try to learn more about intermediate uses, and I'll start here first next time. Hope you keep cranking out even more episodes!

jamesferguson
Автор

Awesome video Arjan! I personally use `Path(__file__)` a lot to build paths relative to the script that's calling it.

jessiehopper
Автор

4:40 that’s why I use Pathlib. Makes for beautiful code (especially relative to the alternative)

Mutual_Information
Автор

I have been using pathlib for a bit over a year now and I like it very much. Better security and processing, I have however, noticed it is slightly slower than using os functions, but that it is not enough to make much of a difference unless you are running a looping function doing a lot of pathlib processes, but then again if you are doing that it is probably code you should refactor anyway.

davidbohlin
Автор

Wow, you made me feel like an expert for a second. Big thanks. The amount of times I learn new things from your videos is really high. Big fan, top-notch content in terms of quality. Today, it must be one of the first times I see a video on something that I discovered 6 months ago. I will still watch it till the end, just to see how you apply it.

wsewlal
Автор

FINALLY! Was way overdue. Happy you finally learned about pathlib. Been loving it for a while now.

TimoRJensen
Автор

As always great content. Learned a lot in just a few minutes. 👏

davidjnevin
Автор

I use Path from Python 3.6 and I didn't know about read_text and write_text! Nice! Thank you Arjan! I was using `with open(file, 'r') as f: lines = f.readlines()` ...

songokussjcz
Автор

Pathlib, i use it a lot, but operator overload was a great idea for my current code! Thanks Arjen

PedigreeGuitars
Автор

Thank you for this Path tutorial Arjan! 🙏
I especially liked the part about Path support in Pydantic and the part about operator overloading.

silkogelman
Автор

Wow Arjan, that's a lovely holiday picture.

haraldtopfer
Автор

I really needed this. Thanks for taking the time to record it and explain things in an interesting, easy-to-understand way.
Only gripe would be needing a clear screen in between demoing each command; but that's just a pet peeve of mine.

kennethbass
Автор

Awesome video Arjan! You are a very good source for "voortschrijdend inzicht" :)

Pjotterbolte
Автор

One of my favorites is Path.parents which returns a parents object (I think it's ultimately a generator) with all of the parent parts.

mrrobotman
Автор

I honestly should probalby use Pathlib more than i do. I don't do a ton on the FS so i get pretty lazy with it, but it Pathlibs ability to make dynamic file paths is so much better than f-strings.

grantwilliams
Автор

Pandas query magic? Think I’ve forgotten that issue.
Great video. Thank you!

thisoldproperty
Автор

Many people still don't know about pathlib and struggle with os.path.sep. Always a mess to read.

You don't need Path.cwd in your cases. Just stay relative and let the OS handle finding the files. Also touch() and then write_text() is not a great example because write_text creates the file anyway.

sausix
Автор

Nice topic. touch is great for creating empty files. write_text will create the file if it doesn't already exist.

jeancerrien
Автор

Wow! 1.3k in an hour, I remember when this was just 100 in the first hour. Arjan getting that 🎂 must be the Dutch Brötchen and Gouda Kaas

basedmuslimbooks