This is the 12th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Review and

Python’s modules for file I/O operations have been covered previously

  • Operating on files and directories – OS module
  • Operating on file paths -os.path module
  • Move archive for file/directory deletion advanced operation -shutil module

In the module above, we have a certain grasp of the file/directory operation, let’s first strengthen a little knowledge before we start to learn (โš ๏ธ : if you are familiar with the absolute path and relative path, can skip to see directly).

๐ŸŒŸ small knowledge: absolute path and relative path

  • Absolute path: Always start from the root folder

    (1) In the Window system, the root folder is drive letters (C:, D:).

    (2)OS X or Linux with/as the root folder.

  • Relative path: Indicates the location of the file relative to the current working directory.

    (1).\ indicates the current directory

    (2).. \ indicates the parent directory of the current directory

๐ŸŽต๐ŸŽต Without further discussion, Let’s learn about the pathlib module

1. Overview of pathlib module

The Pathlib module provides a set of classes for file system paths whose semantics are applicable to different operating systems.

The pathlib module provides the following diagram of related classes:

๐Ÿ”” The connection of the haircut indicates that there is inheritance relationship. For example, PurePath is the parent class of PurePosixPath and PureWindowsPath

๐Ÿ“ข Important notes:

  • The pathlib module has two main classes: Purepath and Path.

  • The PurePath class treats the path as a normal string.

    (1) Can concatenate multiple strings into the current system path format

    (2) It can judge whether any two paths are equal

    (3) PurePath only operates the path and does not determine the authenticity of the path

    (4) PurePath has two subclasses, PurePosixPath for operating UNIX (MAC OS) system paths, PurewindowsPath for operating Windows system paths

  • The Path class operates on real paths.

    (1) The Path class provides a method to determine whether a Path exists

    (2) Path also has two subclasses, the PosixPath class for UNIX (MAC OS) system paths, and the WindowsPath class for Windows system paths

  • The fundamental difference between purePath and Path is

    (1) The essence of purepath is still a string

    (2) Path actually accesses the underlying file path, so it provides properties and methods to access the underlying file system methods

๐Ÿ”” UNIX system path format: the root path is a slash (/), and the path separator is also a slash (/). Windows system path format: The system root path is a drive letter (such as C:), and the delimiter is a backslash (\).

2. Related methods of Pathlib module

The Pathlib module provides PurePath, Path, and their subclasses, a total of six class modules

class role
Pathlib.PurePath Pure path: indicates the path format of the current system
Pathlib.PurePosixPath A subclass of pure paths, for UNIX systems
Pathlib.PureWindowsPath A subclass of pure paths, for Windows systems
Pathlib.Path A subclass of pure paths that operates on real paths
Pathlib.PosixPath Subclass of Path and PurePosixPath for UNIX systems
Pathlib.WindowsPath Subclass of Path and PureWindowsPath, for Windows systems

The ๐Ÿ“ฃ PuerPath class provides the following main instance methods/properties:

Method/attribute role
PurePath.drive Returns the drive letter in the path string.
PurePath.root Returns the root path of the path string
PurePath.anchor Returns the drive letter and root path of the path string
PurePath.parents Returns all parent paths of the current path
PurePath.name Returns the file name of the current path
PurePath.suffixes Returns a list of file extension names in the current path
PurePath.suffix Returns the current file extension
PurePath.as_posix() Switch to UNIX system path format (slash)
PurePath.as_uri() The current path is converted to a URL. Only absolute paths are supported
PurePath.is_absolute() Check whether it is an absolute path
PurePath.joinpath(*other) Link multiple paths together (slash/hyphen)
PurePath.match(pattern) Checks whether the current path matches the specified wildcard.
PurePath.relative_to(*other) Calculates the path result of the current path relative to other
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, ValueError is raised.
PurePath.with_suffix(suffix) Replace the file name extension in the current path with the new name extension. If there is no suffix in the current path, a new suffix is added.

The ๐Ÿ“ฃ Path class provides the following primary instance methods/properties:

methods role
Path.cwd() Returns the current directory path
Path.stat() Return information about the path
Path.chmod() Change the mode and permissions of the file
Path.exists() Whether the file path exists
Path.mkdir() New key to the directory of the path
Path.glob(pattern) The parse path for the given symbol
Path.group() Return the user group of the file
Path.is_dir() Whether the path is a directory
Path.is_file() Check whether the path points to a file
Path.is_mount() Check whether the directory is mounted. This parameter is applicable only to UNIX systems
Path.is_symlink() Determine if the path points to a symbolic link

3. Experiment

The pathlib module provides a much simpler way to manipulate the path than os.path.

Import pathlib # purepath Pure = pathlib.purepath ('f:\JueJin\juejin.txt') print("purepath: ",Pure) print("Pure root:",Pure. Root) print("Pure Parents:",Pure. Parents) print("Pure extension :",Pure. Suffixes) print("Pure Type: ",type(Pure)) Pa = pathlib.Path("f:\JueJin\juejin.txt") print("Path: ",Pa) print("Pa root: ",Pa) ", pa.root) print("Pa parents :", pa.parents) print("Pa extension :", pa.suffixes) print("Pa type :", pa.root) print("Pa parents :", pa.parents) print("Pa extension :", pa.suffixes) ",type(Pa)) # Path print("Pa exists:", pa.exists ()) print("Pa is_dir:", pa.is_dir ()) print("Pa is_file:",Pa.is_file())Copy the code

conclusion

In this installment, we look at the Pathlib module’s operations on paths used in various operating systems, with two key points:

  • PurePath indicates a PurePath. The path is an ordinary string and is used only to manipulate the path. The authenticity of the path is not determined
  • The path class provides a way to determine if a path is real or valid

If the work project needs to migrate multiple systems, in the need to operate the file path, we can use the PathLib module, convenient and concise, easy project maintenance.

See you next time ~แƒฆ(ยด ยท แด— ยท ‘) ๐ŸŒน๐ŸŒน๐ŸŒน