使用python 更改文件内容

来源:互联网 发布:低碳饮食 知乎 编辑:程序博客网 时间:2024/06/06 20:46

由于在一个把ads下的arm 工程项目移植到gnu项目时候需要做大量重复的修改,比如把

[plain] view plaincopyprint?
  1. ABC     EQU 1  
修改为:

[plain] view plaincopyprint?
  1. #define     ABC 1  

如果用手工一个个修改很浪费时间,所以就用python脚本来做这些工作,发现很容易就搞定了(以前遇到类似问题总是用c代码来写,代码量很多而且容易出错!!)

 

源代码如下:

[python] view plaincopyprint?
  1. def func():  
  2.         ffrom=open("2440init.s","r")  
  3.         fto=open("2440init1.s","w")  
  4.         while True:  
  5.                 l = ffrom.readline()  
  6.                 if not l:  
  7.                         break  
  8.                 if 'EQU' in l:  
  9.                         temp = l.split("EQU")  
  10.                         temp1 = '#define  ' + temp[0] + temp[1]  
  11.                         #print temp1  
  12.   
  13.                         fto.write(temp1)  
  14.                 else:    
  15.                         temp1 = l  
  16.                         fto.write(temp1)  
  17.   
  18. if __name__ == "__main__":  
  19.         func()  

用一个文件 2440init.s 来测试下:

[plain] view plaincopyprint?
  1. abc             EQU             1  
  2. pds             EQU             9  

最终生成的文件2440init1.s 内容如下所示:

[plain] view plaincopyprint?
  1. #define  abc                            1  
  2. #define  pds                            9  


 前面既然说了是替换文件的内容 ffrom 跟 fto 打开的应该是同一个文件,但是发现 写文件输出流打开后,会自动清空文件(append模式除外) 貌似和java表现一样的。

可以用如下代码完成

[python] view plaincopyprint?
  1. def func():  
  2.         input   = open("2440init.s")  
  3.         lines   = input.readlines()  
  4.         input.close()  
  5.   
  6.         output  = open("2440init.s",'w');  
  7.         for line in lines:  
  8.                 #print line  
  9.                 if not line:  
  10.                         break  
  11.                 if 'EQU' in line:  
  12.                         temp    = line.split("EQU")  
  13.                         temp1   = '#define  ' + temp[0] + temp[1]  
  14.                         output.write(temp1)  
  15.                 else:    
  16.                         output.write(line)  
  17.         output.close()  
  18.   
  19. if __name__ == "__main__":  
  20.         func()  


    如果一个比较大的工程文件,需要遍历工程中的每一个文件。如果文件中包含指定的字符串比如说 #include "appdef.h"  则将之替换为 #include "datatype.h" :

[python] view plaincopyprint?
  1. import os  
  2.   
  3. def direc():  
  4.     for d,fd,fl in os.walk('/home/shichao/gun-ucos'):  
  5.             for f in fl:  
  6.                     sufix = os.path.splitext(f)[1][1:]  
  7.             if ( (sufix == 'h'or (sufix == 'c') ):  
  8.                 #print sufix  
  9.                 func(d + '/' + f)  
  10.   
  11.   
  12. def func(filename):  
  13.     input   = open(filename)  
  14.     lines   = input.readlines()  
  15.     input.close()  
  16.   
  17.     output  = open(filename,'w')  
  18.     for line in lines:  
  19.         if not line:  
  20.             break  
  21.         if (('appdef.h' in line) and ('include' in line) ):  
  22.             temp    = line.split("appdef")  
  23.             temp1   = temp[0] + 'datatype' + temp[1]  
  24.             output.write(temp1)   
  25.         else:  
  26.             output.write(line)    
  27.     output.close()  
  28.   
  29. if __name__ == "__main__":  
  30.     direc()  
0 0