Home >Backend Development >Python Tutorial >Analyze the use of pathlib

Analyze the use of pathlib

高洛峰
高洛峰Original
2017-03-27 15:52:592796browse

pathlib only appeared after version path3.x. It has more powerful functions and is easier to operate directories. If you use python3.x to operate directories, you can consider using this module first.

import pathlib ---- > Just a few functions used this time to facilitate memory

1. relative_to

Path1 = Path("/home/dante")

path2 = Path("/ home/dante/project/src")

 path2.relative_to(path1) ----------> project/src Remove the same parts in the path1 and path2 directories
2. glob and fnmatch

Glob is an attribute of pathlib.Path. After returning a generator loop, you can return it through stem (get the file name), suffix (suffix, return a string), suffixes--> The list is list

 fnmatch is a separate library that filters keywords and returns bool values

3. pathlib reads files 3.5.x new features

pathlib.path ('filename').read_text() can read the file directly

The above is the detailed content of Analyze the use of pathlib. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn