The Pathlib Module operates on paths used in various operating systems (such as paths that specify file locations, including absolute and relative paths). Here is a brief introduction to the specific functions of several classes included in the following figure:
Note that the format of the path is completely different on the UNIX operating system and the Windows operating system. The main difference is the root path and the path separator. The root path of the UNIX system is a slash (/), while the root path of the Windows system is a drive letter. (C :); the delimiter for UNIX system paths is a slash (/) and Windows uses a backslash (\).
The PurePath class (as well as the PurePosixPath and PureWindowsPath classes) provides a number of constructors, instance methods, and class instance properties for our use.
It should be noted that when using the PurePath class, considering the differences in operating systems, if you use PurePath to create objects on UNIX or Mac OS X systems, the constructor of this class actually returns PurePosixPath objects; otherwise, if you are on a Windows system to create an object using PurePath on the class, and the constructor of this class returns a PureWindowsPath object.
Of course, we can directly use the PurePosixPath class or PureWindowsPath class to create class objects used by the specified operating system.
[Example] Execute the following statement on a Windows system:
from pathlib import * # Create PurePath, actually use PureWindowsPath path = PurePath ('a.txt') print (type (path))
The output is:
[class 'pathlib.PureWindowsPath']
Obviously, on the Windows operating system, a PureWindowsPath class object is created using the PurePath class constructor.
In addition, when PurePath creates an object, it also supports passing in multiple path strings, which will be spliced into a path format string. Example:
from pathlib import * # Create PurePath, actually use PureWindowsPath path = PurePath ('http', 'www.freelearningpoints.com', 'python') print (path)
The output is:
http\www.freelearningpoints.com\python
As you can see, because the machine is a Windows system, the output here is a path for Windows platforms. If you want to output UNIX-style path strings on Windows systems, you need to use the PurePosixPath class. Example:
from pathlib import * path = PurePosixPath('http', 'www.freelearningpoints.com', 'python') print(path)
The output is:
http/www.freelearningpoints.com/python
One thing to mention about the value is that if you don't pass any parameters when using the PurePath class constructor, it is equivalent to passing in the point '.' (For the current path) as a parameter. Example:
from pathlib import * path = PurePath() print(path) path = PurePath('.') print(path)
The output is:
.
.
In addition, if multiple parameters are passed to the PurePath constructor, including multiple root paths, only the last root path and subsequent subpaths will take effect. Example:
from pathlib import * path = PurePath('C://','D://','a.txt') print(path)
The output is:
D:\a.txt
Note that for Windows-style paths, only drive letters (such as C, D, etc.) can be counted as root paths.
It should be noted that if the parameter passed to the PurePath constructor contains extra slashes or dots (., representing the current path), it will be ignored directly. For example:
from pathlib import * path = PurePath('C://./a.txt') print(path)
The output is:
C:\a.txt
The PurePath class also overloads various comparison operators. For path strings of the same style, you can determine whether they are equal or compare the size (actually, compare the size of the strings); for path strings of different styles you can only judge whether they are equal (obviously, they cannot be equal), but you cannot compare sizes.
[Example:]
from pathlib import * # Unix-style paths are case sensitive print (PurePosixPath ('C: //a.txt') == PurePosixPath ('c: //a.txt')) # Windows-style paths are not case sensitive print (PureWindowsPath ('C: //a.txt') == PureWindowsPath ('c: //a.txt'))
The output is:
False
True
More special is that PurePath class objects support the direct use of slashes (/) as connectors between multiple strings, for example:
from pathlib import * path = PurePosixPath('C://') print(path / ‘a.txt')
The output is:
C:/a.txt
The path constructed in the above way is essentially a string, so we can use str() to convert PurePath objects into strings. Example:
from pathlib import * # Unix-style paths are case sensitive path = PurePosixPath ('C: //','a.txt') print (str (path))
The output is:
C: /a.txt
The following table lists PurePath class instance methods and properties that are commonly used. Because PurePath operates on strings, these instance attributes and instance methods in following table also operate on strings in essence.
Class Instance Properties and Instance Method Names | Function Description |
---|---|
PurePath.parts | Returns the parts contained in the path string. |
PurePath.drive | Returns the drive letter in the path string. |
PurePath.root | Returns the root path in a path string. |
PurePath.anchor | Returns the drive letter and root path in the path string. |
PurePath.parents | Returns all parent paths of the current path |
PurePath.parent | Returns the parent path of the current path, which is equivalent to the return value of parents [0]. |
PurePath.name | Returns the file name in the current path. |
PurePath.suffixes | Returns all suffixes of files in the current path. |
PurePath.suffix | Returns the file suffix in the current path. Equivalent to the last element of the list returned by the suffixes property. |
PurePath.stem | Returns the name of the main file in the current path |
PurePath.as_posix() | Converts the current path to a UNIX-style path. |
PurePath.as_uri() | Converts the current path to a URL. Only absolute paths can be converted, otherwise ValueError will be raised. |
PurePath.is_absolute() | Determines whether the current path is an absolute path. |
PurePath.joinpath(*other) | Join multiple paths together, similar to the slash (/) connector introduced earlier. |
PurePath.match(pattern) | Determines whether the current path matches the specified wildcard. |
PurePath.relative_to(*other) | Get the result after removing the reference path in the current path. |
PurePath.with_name(name) | Replace the file name in the current path with the new file name. If there is no file name in the current path, a ValueError is raised |
PurePath.with_suffix(suffix) | Replace the file extension in the current path with the new extension. If there is no suffix in the current path, a new suffix is added. |
Compared with the PurPath class, the biggest difference between the Path class is that it supports the judgment of the authenticity of the path.
It can be easily seen from Figure 1 that Path is a subclass of PurePath. Therefore, in addition to the various constructors, instance attributes, and instance methods provided by PurePath, the Path class also provides methods to identify the validity of the path string. It can even Determine whether the path corresponds to a file or a folder. If it is a file, you can also read and write files.
Like PurePath, Path also has two subclasses: PosixPath (for UNIX-style paths) and WindowsPath (for Windows-style paths).
More Tutorials:
Python Installation - Linux (Ubuntu)More Python Exercises:
Python String Exercises