code_format.py 865 B

123456789101112131415161718192021222324252627
  1. import os
  2. import subprocess
  3. import re
  4. import sys
  5. def format_all_file(root):
  6. for root, dirs, files in os.walk(root):
  7. # root 表示当前正在访问的文件夹路径
  8. # dirs 表示该文件夹下的子目录名list
  9. # files 表示该文件夹下的文件list
  10. # 遍历文件
  11. for f in files:
  12. if f.endswith('.c') or f.endswith('.h'):
  13. full_path = os.path.join(root, f)
  14. #print("root: %s, path: %s" % (root, full_path))
  15. print(full_path)
  16. command = 'clang-format -style=file -i %s' % full_path
  17. #print(command)
  18. proc = subprocess.run(command, shell=True, stdout=subprocess.PIPE)
  19. if __name__ == '__main__':
  20. format_all_file('.')
  21. #proc = subprocess.run(command, shell=True, stdout=subprocess.PIPE)