Python's pathlib Module

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ก.ย. 2022
  • Python's standard library pathlib module can be used to manipulate paths, and work with the file system independently of the specific operating system the Python app is running on, making one of the best choices for working with files and paths. In addition, it also supports a concept called globbing that can be used to recursively find files matching a specified pattern in the file system.
    #mathbyteacademy #python
    Code for this Video
    ================
    Available in GitHub: github.com/fbaptiste/python-blog
    My Python Courses
    =================
    - Python 3 Fundamentals (introduction to Python)
    www.udemy.com/course/python3-...
    - Python 3 Deep Dive (Part 1 - Functional)
    www.udemy.com/course/python-3...
    - Python 3 Deep Dive (Part 2 - Iteration, Generators)
    www.udemy.com/course/python-3...
    - Python 3 Deep Dive (Part 3 - Hash Maps)
    www.udemy.com/course/python-3...
    - Python 3 Deep Dive (Part 4 - OOP)
    www.udemy.com/course/python-3...

ความคิดเห็น • 24

  • @josephomondi629
    @josephomondi629 หลายเดือนก่อน

    Dude you don't know how long I've looked for a good video on this module. Thank you so so much and may you keep up the good work

  • @wisenielsmuronha3405
    @wisenielsmuronha3405 2 หลายเดือนก่อน

    The best channel to learn python

  • @chrisyoung7704
    @chrisyoung7704 4 หลายเดือนก่อน

    Thanks, Fred! Clear and detailed explainations!

  • @AlexanderDemin
    @AlexanderDemin ปีที่แล้ว +1

    Thanks, Fred, for bringing it in. Despite heavy using Path for files for a long time, I personally severely underestimated how convenient Path actually is for URLs. Way better instead of:
    good_url = '/' + some_url.strip('/') + '/myendpoint'
    to say:
    good_url = Path('/') / Path(some_url).name / 'myendpoint'
    Not much of being shorter but way more visually pleasant.

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว +2

      Agreed, though often I do not have the Path module loaded in my app at all, and I'll then use f-strings to build up a URL rather than load the module :
      root = some_url.strip('/')
      good_url = f"/{root}/myendpoint"

  • @reneeliu1648
    @reneeliu1648 ปีที่แล้ว +1

    Thank you so much Fred!!! I have learned so much useful/in-depth things from your videos.

  • @joelbeck1822
    @joelbeck1822 ปีที่แล้ว

    Thanks Fred, another great and comprehensive video. I really enjoy your in-depth discussions of individual modules, either from the standard library or third-party libraries! Would love a similar video about the `re` module with some regex magic :)
    Worthy additions are two of my favorite `pathlib` methods: `.read_text()` and `.write_text()`. When starting out with Python it was really puzzling to me why I had to write two lines and a context manager to read a simple text file.
    With `pathlib` it is as simple as `p.read_text(file)` instead of the usual `with p.open() as f: f.read()`

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว +1

      Thanks, glad you like the videos.
      As far as using a context manager for opening files, one major advantage over read_text() (or write_text() for that matter) is that you can read/write a large file in chunks, as opposed to reading it all in memory at once. That way you can read/write files that are very large with little to no impact on your memory/swap file.

    • @joelbeck1822
      @joelbeck1822 ปีที่แล้ว

      @@mathbyteacademy That is a very good point! Thanks for pointing it out.

  • @matinhewing1
    @matinhewing1 ปีที่แล้ว

    Thanks 😊

  • @SuperHamadahmed
    @SuperHamadahmed ปีที่แล้ว +1

    Thanks Fred,
    I would like to see something about os library, and how os.path differs from this and the pros and cons of each

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว +3

      Think of Pathlib as class based higher level alternative to os.path. Think of the difference between the time and datetime modules. You can certainly completely ignore the datetime module and do everything with the lower-level functionality available in the time module, but datetime makes it easier to manipulate dates and times. Same with PathLib.
      For a more thorough rationale and explanation behind the PathLib module see this pep: peps.python.org/pep-0428/

    • @SuperHamadahmed
      @SuperHamadahmed ปีที่แล้ว

      @@mathbyteacademy
      Thanks for your reply and explanation, as always clear and understandable.

  • @yusupgeldiyev637
    @yusupgeldiyev637 ปีที่แล้ว +2

    Fred, thanks for the videos. As a person from math background i enjoy learning from your videos. I would like to ask you if you could create an introductory video about the whole set of software engineering. Let Swe= { {fron end}, {back end}, {data bases} and etc …}.
    I am transitioning from math, but i cant find a video explaining about the whole industry. It would be nice if we had that video which tells us what knowledge is necessary for each subset and amount of knowledge necessary for different experience levels in that subset.
    As of know i dont even know what path to choose, what material to learn, how far to study before applying for a job. If you were starting today (0 cs knowledge, heavy in math) what path would you choose in swe, and what material necessary to learn for that path? It would help a lot.

    • @pocsgeza1711
      @pocsgeza1711 ปีที่แล้ว

      th-cam.com/video/SzJ46YA_RaA/w-d-xo.html

  • @mintxdp
    @mintxdp ปีที่แล้ว

    25:31
    I did not understand how we can manipulate a windows path from mac ?
    Are we manipulating a windows machine from mac with ssh ?

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว +1

      Nope - you can manipulate paths without necessarily being actual paths on a specific computer - that's the difference between abstract and concrete paths.

    • @mintxdp
      @mintxdp ปีที่แล้ว

      @@mathbyteacademy Understood ! Thanks . So, when we are using pureposixpath then it is concrete and when we are working with only "path" then it is abstract . Isn't it?

    • @mathbyteacademy
      @mathbyteacademy  ปีที่แล้ว +1

      Opposite actually - I mention this in the video. You should also look at the docs: docs.python.org/3/library/pathlib.html