Python Tutorial - Pathlib Module

Python Exercise Lists

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:

  • The PurePath class treats a path as an ordinary string. It can concatenate multiple specified strings into a path format suitable for the current operating system and can also determine whether any two paths are equal. Note that with a PurePath operation, it doesn't care if the path is valid.
  • PurePosixPath and PureWindowsPath are subclasses of PurePath. The former is used to manipulate UNIX (including Mac OS X) -style paths, and the latter is used to manipulate Windows-style paths.
  • The Path class is different from the above three classes. The path it operates on must be real and valid. The Path class provides methods to determine whether a path exists.
  • PosixPath and WindowPath are subclasses of Path and are used to manipulate Unix (Mac OS X) -style paths and Windows-style paths, respectively.

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 (\).

Usage of the Python PurePath Class

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.

Python PurePath class constructor

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

Python Purepath Class Instance Properties and Instance Methods

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.

Python Function and Usage of Path Class

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)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises