Python path path related processing

Before version 3.4, the
os.path module
os.path.join('a','b','c') returns a/b/c path string in the current path, which can automatically replace / and \ according to the environment system.
path.exists(p) Check whether p path actually exists
path.split(path.abspath(p)), split p path into parent directory and base directory
path.dirname(p) Return p's parent directory
path.basename(p ) Returns the base directory
of p path.splitdrive(p) Returns the drive letter name of the path P

Starting from version 3.4, it is recommended to use the pathlib module, which provides Path objects to operate, including directories and files.
Path() The current directory
Path('a','b','c/d') The a/b/c/d of the current directory
Path('/a') The

parts attribute of the a directory under the root can be returned to the path Each part of .
joinpath(*others) joins multiple strings into a Path object.
The logical parent directory of the parent directory is the
parent directory sequence, index 0 is the immediate parent directory
name The last part of the directory. Return a string.
The extension of the last section in the suffix directory. Returns an empty string without an extension. Dot
suffixes returns a list of multiple extensions
stem returns the last part of the string without the last extension.
with_suffix(suffix) adds the extension to the end of the path and returns a new path, if the extension exists, it is invalid.
with_name(name) replaces the last part of the directory and returns a new path.
pwd() returns the current working directory.
home() returns the current home directory.
is_dir() is a directory, returns bool
is_file() is a normal file.
is_syslink() is a soft link.
is_socket() is a socket file.
is_block_device() is a block device.
is_char_device() is a character device.
is_absolute() checks if it is an absolute path.
resolve() returns a new path, which is the absolute path of the current path object, if it is a direct resolution of the soft link.
exists() Whether the directory or file exists.
rmdir() deletes empty directories without providing
touch(mode = 0o666, exist_ok = True)
mkdir(mode = 0o777, parents = False , exist_ok = False) parents , whether to create a parent directory, equivalent to mkdir -p; False is, parent If the directory does not exist, FIleNotFouneError is thrown.
exist_ok parameter, when False, the path exists and FileExistError is thrown; when True, the exception is ignored.
iterdir() iterates the current directory.
glob(pattern) globs the given pattern
rglob(pattern) globs the given pattern for recursive directories. What is returned is a generator.
match(pattern) Pattern matching, returns True on success.
stat() is equivalent to the stat command
lstat() If it is a symbolic link, it displays the file information of the symbolic link itself.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324824360&siteId=291194637