#!/usr/bin/env python # -*- encoding: utf-8 -*- ''' @Contact : liuyuqi.gov@msn.cn @Time : 2023/05/03 13:47:45 @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved. @Desc : add header to all markdown files ''' import os,logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') logger = logging.getLogger(__name__) def add_header(): """ param parameter_list: return: """ header='''--- title: 项目介绍 date: 2023-05-02 12:00:00 --- ''' for root, dirs, files in os.walk("../"): for file in files: if file.endswith('.md'): try: with open(os.path.join(root,file),'r+',encoding='utf-8') as f: content=f.read() if content.startswith('---') or content.startswith('\n---'): logger.info('file {} already has header'.format(os.path.join(root,file))) else: f.seek(0,0) f.write(header) f.write(content) logger.info('file {} add header success'.format(os.path.join(root,file))) f.close() except Exception as e: logger.error('file {} add header failed'.format(os.path.join(root,file))) logger.error(e) continue def add_footer(): ''' add footer to all markdown files ''' copyright=''' ## 版权说明: 本文档版权隶属 天问科技 ,**仅用于天问科技旗下公司,团队为客户展示项目案例所用**,任何盗用本公司图文,描述,案例的行为均属违法,我们保留追究法律责任的权利。 ''' for root, dirs, files in os.walk("../"): for file in files: if file.endswith('.md'): with open(file,'a+',encoding='utf-8') as f: content=f.read() if content.find('## 版权说明:')!=-1: logger.info('file {} already has footer'.format(file)) else: f.write('\n'+copyright) f.close() def run(): ''' run ''' add_header() add_footer() if __name__=='__main__': run()