[root@localhost ~]# less txt
123 345 678
accdfesdfdsf
[root@localhost ~]# less tihuan.py
#!/usr/bin/env python
import re
wenjian = open('txt','r+')
a = wenjian.read()
wenjian.seek(0,0)
f = (r'123')
b =re.findall(f,a)
if b != []:
wenjian.write(a.replace(f,'zzh'))
wenjian.close()
[root@localhost ~]# ./tihuan.py
[root@localhost ~]# less txt
zzh 345 678
accdfesdfdsf