python替换文件指定内容
例子,python替换文件内容。
 
#!/usr/bin/env python
#
import os 
os.chdir('d:autotest')# 跳转到文件夹下
casenum=176
filename='case'+str(casenum)+'.txt'
if not os.path.exists(filename):
    print filename,'does not exists'
    exit(1)
oldlines=open(filename).readlines()
newcase=184
while newcase<=200:
    fp=open('case'+str(newcase)+'.txt','w')
    for s in oldlines:
        fp.write(s.replace(str(casenum),str(newcase)))
    fp.close()
    newcase+=1
 
代码比较简单,用到了python os模块,打开文件并读取文件内容,然后用replace替换指定文件的内容。