简体   繁体   中英

What is the difference between Pathlib and os.path

Reading official documentation of both libraries:

os.path

See also The pathlib module offers high-level path objects.

pathlib

See also For low-level path manipulation on strings, you can also use the os.path module.

So os.path is for low-level use and path lib for high level use. But what it means? I don't understand the low vs high in this context.

There are many examples out there about pathlib vs os.path for example Pathlib vs. os.path.join in Python

but in none of them there is explanation of the low vs high concept. Is there example that explain what exactly is low and what exactly is high?

Is it correct that low level means the need to manipulate the path as string object?

I see it as "Use pathlib if you are interested in working with the path as an abstract entity", use "os.path" if you're really interested in the resources represented by that path; in most cases you are interested in the using the 'path' to learn about or handle resources.

High level being 'the path as an abstract entity' low level being 'the resources represented by a path'. While os.path also does have functionality for pathname manipulation etc. it's all there to enable you to get to the resources you're really interested in.

From the pathlib docs:

If you've never used this module before or just aren't sure which class is right for your task, Path is most likely what you need. It instantiates a concrete path for the platform the code is running on.

Pure paths are useful in some special cases; for example:

If you want to manipulate Windows paths on a Unix machine (or vice versa). You cannot instantiate a WindowsPath when running on Unix, but you can instantiate PureWindowsPath.

You want to make sure that your code only manipulates paths without actually accessing the OS. In this case, instantiating one of the pure classes may be useful since those simply don't have any OS-accessing operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM