python往某一行行尾追增内容

示例:在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)

你可能感兴趣的:(python)