python class中插入代码

import re
import os
import glob

# search all files inside a specific folder
# *.* means file name with any extension
dir_path = r'E:/pbrt-v3/src/**\*.*'
for file in glob.glob(dir_path, recursive=True):
    if not file.endswith(('.h', '.cpp', '.c', '.hpp')):
        print("file_name, ", file)
        continue

    file_content = ''
    try:
        with open(file, 'r', encoding='utf-8') as file_obj:
            flag = 0
            for line in file_obj:
                file_content += line            
                if flag == 0 and re.match(r'.*class.*', line):
                    print(line.split(' ')[1])
                    flag = 1
                elif flag ==1 and (re.match(r'.*public:.*', line) or re.match(r'.*private:.*', line) or re.match(r'.*protected:.*', line)):
                    flag = 2
                    file_content += "CLASS_FINED"

                    
    except:
        print('error catched', file)
        continue

    with open(file, 'w', encoding='utf-8') as file_obj:
        file_obj.write(file_content)


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容