""" 使用方法:将该脚本文件放在用例脚本文件目录下,该脚本会遍历得到所在文件夹下所有py文件中的class类名,并将类名改为驼峰命名方式 """ import glob import os import re current_folder = os.getcwd() py_files = glob.glob(os.path.join(current_folder, '*.py')) pattern = re.compile(r'class\s+(\w+)\s*:', re.MULTILINE) for py_file in py_files: with open(py_file,'r', encoding='utf-8') as file: code = file.read() for match in pattern.finditer(code): class_name = match.group(1) code = code.replace(class_name,os.path.basename(py_file)[:-3]) for match in pattern.finditer(code): class_name = match.group(1) code = code.replace(class_name, os.path.basename(py_file)[:-3]) class_name_modified = ''.join(word.capitalize() for word in class_name.split('_')) code = code.replace('class ' + class_name,'class ' + class_name_modified) code = code.replace(class_name + '.__doc__', class_name_modified + '.__doc__') print(f'修改文件类名:{py_file}:{class_name}->{class_name_modified}') with open(py_file,'w', encoding='utf-8') as file: file.write(code)
标签:code,name,批量,py,类名,file,class,驼峰 From: https://www.cnblogs.com/Tanwheey/p/18153350