python note 72 - use pathlib instead of OS path

preface

If you are still worried about operating the file path, you won't use OS Path module, then it's time to try pathlib.

pathlib Library

The pathlib library is from Python 3 From 4 to Python 3 6 has been relatively mature. If you can directly use more than 3.6 for your new project, it is recommended to use pathlib. Compared with the old OS Path has several advantages:

  • The management of the old path operation function is confused. Some are imported into os and some are in os Path, and the new unified usage can be managed with pathlib.
  • The old usage is hard to deal with between different operating systems win, mac and linux. Changing the operating system often requires code changes and additional operations.
  • The old usage is mainly in the form of function, and the returned data type is usually string. However, the path and string are not equivalent, so when using os operation path, other class libraries are often introduced to assist the operation. The new usage is object-oriented, which is more flexible and convenient to handle.
  • pathlib simplifies many operations and makes it easier to use.

Comparison of commonly used pathlib and os

operationos and os.pathpathlib
Absolute pathos.path.abspathPath.resolve
Modify permissionsos.chmodPath.chmod
Create directoryos.mkdirPath.mkdir
renameos.renamePath.rename
moveos.replacePath.replace
Delete directoryos.rmdirPath.rmdir
Delete fileos.remove, os.unlinkPath.unlink
working directoryos.getcwdPath.cwd
Does it existos.path.existsPath.exists
User directoryos.path.expanduserPath.expanduser and Path.home
Is it a directoryos.path.isdirPath.is_dir
Is it a fileos.path.isfilePath.is_file
Is it a connectionos.path.islinkPath.is_symlink
File propertiesos.statPath.stat, Path.owner, Path.group
Is it an absolute pathos.path.isabsPurePath.is_absolute
Path splicingos.path.joinPurePath.joinpath
file nameos.path.basenamePurePath.name
Parent directoryos.path.dirnamePurePath.parent
File with the same nameos.path.samefilePath.samefile
suffixos.path.splitextPurePath.suffix

pathlib get file path

Path.cwd get current folder path

Note that the returned object is not a string, but a WindowsPath object

from pathlib import Path

# 1. Class methods can be called directly cwd()
print(Path.cwd())  # C:\Users\dell\PycharmProjects\untitled3\demo

# 2. can also be instantiated and invoked.
p = Path('./')
print(p.cwd()) # C:\Users\dell\PycharmProjects\untitled3\demo
print(type(p.cwd()))  # <class 'pathlib.WindowsPath'>

Get current file path

from pathlib import Path

# Current file path
p = Path(__file__)
print(p)

Gets the absolute Path of the Path object

from pathlib import Path

# Current file path
p = Path('data.json')
print(p)  # data.json object
print(p.absolute())  # C:\Users\dell\PycharmProjects\untitled3\demo\data.json

Some commonly used get file attributes

from pathlib import Path

# Current file path
p = Path(__file__)
print(p.absolute())   # Get absolute path
print(p.resolve())    # Get absolute path
print(p.name)   # Get file name 'a1117 py'
print(p.stem)    # Just the file name, not the suffix a1117
print(p.suffix)  # Get file suffix py
print(p.suffixes)  # File all monkeys ['. py']
print(p.parts)  # Split ('c: \ \ ','users',' Dell ',' pycharmprojects', 'Untitled3', 'demo', 'a1117. Py')
print(p.parent)  # C:\Users\dell\PycharmProjects\untitled3\demo
print(p.parent.parent)  # C:\Users\dell\PycharmProjects\untitled3
print(p.parents)  # All parents < windowspath parents>
print(p.anchor)  # Anchor, the front part of the directory C: \ or/

Get upper level directory

from pathlib import Path

# . parent get the previous layer
print(Path.cwd().parent)

# After instantiation, call. parent
p = Path('./')
print(p.cwd().parent)

Get the upper layer using chained method calls parent.parent

from pathlib import Path

# . parent get the previous layer
print(Path.cwd().parent.parent)

# After instantiation, call. parent
p = Path('./')
print(p.cwd().parent.parent)

Get user home directory

from pathlib import Path
print(Path.home())  # c:\Users\dell

Judgment file, folder

is_file() determines whether it is a file

from pathlib import Path

# 1.  is_file() determines whether it is a file
print(Path.cwd().is_file())  # False


# 2. can also be instantiated and invoked.
p = Path('./data.json')
print(p.is_file())  # True

is_dir() determines whether it is a folder

from pathlib import Path

# 1.  is_file() determines whether it is a file
print(Path.cwd().is_dir())  # True


# 2. can also be instantiated and invoked.
p = Path('./data.json')
print(p.is_dir())  # False

exists() determines whether a file or folder exists

from pathlib import Path

# exists() to determine whether it exists
p = Path('./data.json')
print(p.exists())  # True or False

is_ Determine whether the absolute path is absolute

from pathlib import Path

# Current file path
p = Path(__file__)
print(p)
print(p.is_absolute())  # True

joinpath splicing directory

You can use something like OS path. Join method

from pathlib import Path

# Current file path
p = Path('./')
print(p.absolute())  # C:\Users\dell\PycharmProjects\untitled3\demo
print(p.joinpath('data.json'))  # data.json
print(p.joinpath('data.json').absolute())   # C:\Users\dell\PycharmProjects\untitled3\demo\data.json
# Splicing multilayer
print(p.joinpath('files', 'data.json'))   # files\data.json
print(p.joinpath('files', 'data.json').absolute())  # C:\Users\dell\PycharmProjects\untitled3\demo\files\data.json

pathlib supports / concatenation of paths. Few people use this syntax to estimate

from pathlib import Path

# Current file path
p = Path('./')
# /Splicing
new_path = p / 'files' / 'data.json'
print(new_path.absolute())

iterdir() traverses the file directory

For example, there are the following folders and sub files in the files directory of the current script

. iterdir() traverses all paths (files and subdirectories) under a directory

from pathlib import Path

# Current file path
p = Path('files')
for i in p.iterdir():
    print(i.absolute())

"""Operation results:
C:\Users\dell\PycharmProjects\untitled3\demo\files\json
C:\Users\dell\PycharmProjects\untitled3\demo\files\username.txt
C:\Users\dell\PycharmProjects\untitled3\demo\files\yaml
"""

If you only need to get the folder, you can add a judgment is_dir()

from pathlib import Path

# Current file path
p = Path('files')
print([i for i in p.iterdir() if i.is_dir()])  # [WindowsPath('files/json'), WindowsPath('files/yaml')]

It can also be used is_file get file object

from pathlib import Path

# Current file path
p = Path('files')
print([i for i in p.iterdir() if i.is_file()])  # [WindowsPath('files/username.txt')]

glob() and rglob() pattern matching (regular expression)

Use pattern matching (regular expression) to match the specified path. glob will only match the current directory, and rglob will recurse all subdirectories
For example, there are the following folders and sub files in the files directory of the current script

glob will only match the current directory

from pathlib import Path

p = Path('files')
# glob will only traverse to find the current directory
print(p.glob('*.txt'))  # <generator object Path.glob at 0x000001A44565A518>
print([i for i in p.glob('*.txt')])  # [WindowsPath('files/username.txt')]
print([i for i in p.glob('*.yml')])  # []

rglob recurses all subdirectories

from pathlib import Path

p = Path('files')
# glob will only traverse to find the current directory
print(p.rglob('*.txt'))  # <generator object Path.glob at 0x000001A44565A518>
print([i for i in p.rglob('*.txt')])  # [WindowsPath('files/username.txt')]
print([i for i in p.rglob('*.yml')])  # [WindowsPath('files/yaml/aa.yml'), WindowsPath('files/yaml/bb.yml')]

match() checks whether the path conforms to the rule

from pathlib import Path

p = Path('data.json')
# math check matching rules
print(p.match('*.json'))  # True

Create file operation

touch() create file

from pathlib import Path

p = Path('xx.json')
p.touch()   # Create a XX json

When the file already exists, p.touch() will not report an error, because the default parameter is exist_ok=True
If exist is set_ OK = false, if the file already exists, touch will report an error

from pathlib import Path

p = Path('xx.json')
p.touch(exist_ok=False)   # Create a XX json

Throw exception FileExistsError: [Errno 17] File exists: 'XX json’

mkdir() create directory

Create a yoyo directory under the current script

from pathlib import Path

p = Path('yoyo')
# mkdir create yoyo directory
p.mkdir()

If you want to create a multi-level directory 'yoyo/json' at one time

from pathlib import Path

p = Path('yoyo/json')
# mkdir create yoyo/json directory
p.mkdir()

An exception filenotfounderror will be thrown: [winerror 3] the system cannot find the specified path‘ yoyo\json’

Create directory MKDIR recursively (parents = true)

from pathlib import Path

p = Path('yoyo/json')
# mkdir create yoyo/json directory
p.mkdir(parents=True)

Delete file operation

Deleting a directory is very dangerous and there is no prompt. Be careful

rmdir() deletes only one level of directory at a time, and the current directory must be empty.

from pathlib import Path

p = Path('yoyo/json')
# mkdir create yoyo/json directory
p.rmdir()

unlink() delete file

from pathlib import Path

p = Path('files/username.txt')
p.unlink()

File read / write operation

pathlib simply encapsulates reading and writing, and there is no need to repeatedly open files and manage the closing of files.

  • .read_text() read text
  • .read_bytes() read bytes
  • .write_text() write text
  • .write_bytes() writes bytes
from pathlib import Path

p = Path('yo.txt')
p.write_text("hello world")
print(p.read_text())  # hello world

file. The write operation uses w mode. If there is a file content before, it will be overwritten.

Modify file

replace() move files

from pathlib import Path

p = Path('yo.txt')
p.write_text("hello world")
print(p.read_text())  # hello world

p.replace('xx.json')

with_name() rename file

from pathlib import Path

p = Path('hello.txt')
p.write_text("hello world")
print(p.read_text())  # hello world

# Rename to a new file object
new_file = p.with_name('x.txt')
print(new_file)
p.replace(new_file)  # Move to new location

Keywords: Python

Added by codecontractor on Sun, 06 Mar 2022 03:11:58 +0200