安装
pip install black
使用
$ black {source_file_or_directory}
特殊用法
使用 # fmt:on/off 来标记不需要format的代码,fmt : on/off 的缩进必须相同
demo
一个可以format整个项目的demo
# -*- coding: utf-8 -*-
# ---
# @Software: PyCharm
# @File: formatter.py
# @Author: yanggen
# @E-mail: zhull991122@sjtu.edu.cn
# @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])