Python file and path manipulation artifact: pathlib

pathlibPurePathThe and class are encapsulated in the Pathformer, and the former is used to handle path-style strings; the latter is a subclass of the former, which can directly handle paths.

PurePathThe sweetest feature is overloading the operator, so that you can achieve something like the following

>>> from pathlib import Path, PurePath
>>> pp = PurePath("E:/")
>>> pp
PureWindowsPath('E:/')
>>> pp/"test"
PureWindowsPath('E:/test')

For PurePathobjects, the following members or member functions can be called

part drive[drive letter], root[root path], anchor[drive letter + root path],
parent[parent path], parents[all parent paths], name[file name]
suffix[file suffix], suffixes[all file suffix], stem[main file name]
dismantle parts[Split], joinpath[Splice]
replace with_name[filename], with_suffix[suffix], with_stem[main filename]
style transfer as_posix(),as_uri()
judge is_absolute, is_relative_to, is_reserved

Among them, the main file name is the file name after removing the suffix.

PurePathThere are also some slightly more complex functions in the class,

>>> pp = PurePath("E:\Code\test.py")
# 用于匹配文件后缀
>>> pp.match("*.py")
True
# 去除基准路径
>>> pp.relative_to("E:\\")
PureWindowsPath('Code\test.py')
# 更改文件名
>>> pp.with_name('test1.py')
PureWindowsPath('E:/test1.py')
# 更改后缀名
>>> pp.with_suffix(".md")
PureWindowsPath('E:/Code\test.md')
# 更改主文件名
>>> pp.with_stem("help.md")
PureWindowsPath('E:/help.md.py')

Path

Path is a subclass of PurePath, on PurePaththe basis of which some judgment functions are added.

decision function is_diris_fileis_fifo
is_block_deviceis_char_device
is_mountis_symlinkis_socket

PathObjects can also be opened and written to, but hopelessly there are no closefunctions, so it's not recommended. In other words, this Path.openis not like it is used by programmers, because Pathit provides a more convenient way of reading and writing read_bytes, read_textand write_bytes, write_text.

E.g

>>> p = Path(r'E:\Documents\00\0324\Test.txt')
>>> p.write_text("hello world")
11
>>> p.read_text()
'hello world'
>>>

In addition, it can be used to directly manipulate files and folders, providing very powerful file processing functions. The functions provided in and correspond functionally as Pathfollowsos

os. pathlib Function
chmod() chmod() Change file properties
mkdir()
os.makedirs()
mkdir() new folder
rename() rename() Rename
replace() replace() Rename, overwrite if new name already exists
rmdir() rmdir() delete folder
remove()
unlink()
unlink() Delete Files
getcwd() Path.cwd() current path
path.abspath() resolve() absolute path
path.expanduser() expanduser()
home()
system/user path
listdir () iterdir () list all subpaths
path.exists() exists() Determine if the path exists
path.isdir() is_dir() Determine if a path is a directory
path.isfile() is_file() Determine if the path is a file
path.islink() is_symlink() Determine if a path is a link
path.isabs() is_absolute() Determine if the path is an absolute path
link() hardlink_to() link file
symlink() symlink_to() link file
readlink() readlink() read connection
path.join() joinpath() connection path
path.samefile() samefile() Check if the paths are the same
path.basename() name
path.dirname() parent
path.splitext() suffix

Guess you like

Origin blog.csdn.net/m0_37816922/article/details/123715364