python往某一行行尾追增内容

来源:互联网 发布:淘宝的最大股东是谁 编辑:程序博客网 时间:2024/05/01 13:07

示例:在PATH开头的这一行末尾添加/usr/local/mysql/bin

with open('/home/mysql/.bash_profile') as f:

    lines=f.readlines()
with open('/home/mysql/.bash_profile','w') as w:
    for l in lines:
        if(l.startswith('PATH')):
            w.write(l.replace(b'\n',b':/usr/local/mysql/bin\n'))
        else:
            w.write(l)
0 0