full.py -------------------------------------------------------- # coding=utf-8 import os def format_file(input_file): # 读出文件内容 file_object = open(input_file, 'r') file_content = file_object.readlines() file_object.close() cbit_flag = 0 full_flag = 0 buff = '' for i in range(0, len(file_content)): # 空行不做处理 if ('' != file_content[i].strip()): # 给Cbit前面添加注释 if (1 == cbit_flag): cbit_flag = 0 if (-1 == file_content[i].find('//')): file_content[i] = '//' + file_content[i] #print "file_content[i] = ", file_content[i].strip() # 去掉Full前面的注释 if (1 == full_flag): full_flag = 0 if (0 == file_content[i].find('//')): file_content[i] = file_content[i].strip('/ ') #print "file_content[i] = ", file_content[i] if (file_content[i].find('/* Cbit */') == 0): cbit_flag = 1 if (file_content[i].find('/* Full */') == 0): full_flag = 1 buff += file_content[i] # 以可写属性打开文件 output_file = input_file file_object = open(output_file, 'w') file_object.write(buff) file_object.close() if __name__ == '__main__': cwd = os.getcwd() for (path, dirs, files) in os.walk(cwd): for filename in files: if os.path.splitext(filename)[1] == '.c': if (filename.find('dsptnl.c') != -1): filename = os.path.join(path,filename) print filename format_file(filename) cbit.py -------------------------------------------------------- # coding=utf-8 import os def format_file(input_file): # 读出文件内容 file_object = open(input_file, 'r') file_content = file_object.readlines() file_object.close() cbit_flag = 0 full_flag = 0 buff = '' for i in range(0, len(file_content)): # 空行不做处理 if ('' != file_content[i].strip()): # 去掉Cbit前面的注释 if (1 == cbit_flag): cbit_flag = 0 if (0 == file_content[i].find('//')): file_content[i] = file_content[i].strip('/ ') #print "file_content[i] = ", file_content[i] # 给Full前面添加注释 if (1 == full_flag): full_flag = 0 if (0 != file_content[i].find('//')): file_content[i] = '//' + file_content[i] #print "file_content[i] = ", file_content[i] if (file_content[i].find('/* Cbit */') == 0): cbit_flag = 1 if (file_content[i].find('/* Full */') == 0): full_flag = 1 buff += file_content[i] # 以可写属性打开文件 output_file = input_file file_object = open(output_file, 'w') file_object.write(buff) file_object.close() if __name__ == '__main__': cwd = os.getcwd() for (path, dirs, files) in os.walk(cwd): for filename in files: if os.path.splitext(filename)[1] == '.c': if (filename.find('dsptnl.c') != -1): filename = os.path.join(path,filename) print filename format_file(filename)
python修改文件内容文件放在哪里_python 修改文件内容,进行文件操作
本文介绍了一种用于处理C/C++源代码中特定注释的Python脚本。该脚本可以为某些注释添加前缀或将已有注释去除,并针对包含特定字符串的行进行操作。适用于对名为dsptnl.c的文件进行批量处理。

被折叠的 条评论
为什么被折叠?



