The use of formatting tool BLACK

installation

pip install black

use

$ black {
    
    source_file_or_directory}

Special usage

Use # fmt:on/off to mark code that does not require format, and the indentation of fmt: on/off must be the same

demo

A demo that can format the entire project

# -*- coding: utf-8 -*-
# ---
# @Software: PyCharm
# @File: formatter.py
# @Author: yanggen
# @E-mail: [email protected]
# @Time: 01-18-2021
# ---
import os
import sys


def main(path):
    for (root, dirs, files) in os.walk(path, topdown=True):
        for file in files:
            if file[-3:] == ".py":
                whole_path = os.path.join(root, file)
                os.system("black {}".format(whole_path))


if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage : python formatter.py path_to_project_dir")
        exit(1)
    main(sys.argv[1])

Guess you like

Origin blog.csdn.net/weixin_44602409/article/details/112663622