利用python在文件中的指定位置写入

import os  
file = open( "a.txt", "r" )  
file_add = open("a.txt","r")  
content = file.read()  
content_add = file_add.read()  
pos = content.find( "buildTypes")
if pos != -1:  
        content = content[:pos] + content_add + content[pos:]  
        file = open( "a.txt", "w" )  
        file.write( content )   
        file.close()  
        file_add.close()  

与find用法相同的还有rfind方法,不同的是rfind方法是从文件末尾开始搜索。


版权声明:本文为dfhxunuo原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。