def repalceFile(filePath,strOld,strNew):
properties = open(filePath,'rb+')
lines = properties.readlines()
d=""
for line in lines:
c = line.replace(strOld, strNew)
d += c
properties.seek(0) #不要让python记住执行到这里,从文件头还始
properties.truncate() #清空文件
properties.write(d)
properties.close()
f __name__=='__main__':
conn = getConnect(db,passwd,user,host,port,charset)
testQuery = findTestQuery(conn)
for test in testQuery:
image_old =test[2]
image_new=test[1]
repalceFile("F:\\pythonTest\\Test_image\\Test.sql",image_old,image_new)
python 处理文件,替换文件中的内容def repalceFile(filePath,strOld,strNew): properties = open(filePath,'rb+') lines = properties.readlines() d="" for line in lines: c = line.replace(strOld