python修改文件内容文件放在哪里_python 修改文件内容,进行文件操作

本文介绍了一种用于处理C/C++源代码中特定注释的Python脚本。该脚本可以为某些注释添加前缀或将已有注释去除,并针对包含特定字符串的行进行操作。适用于对名为dsptnl.c的文件进行批量处理。

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)

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值