import io, sys p = r'C:\Users\locpham\SonicForgeStudio\native_bridge\src\gui_probe.cpp' s = io.open(p, encoding='utf-8', newline='').read() old = 'EXCEPTION: %s\r\n", e.what())' new = 'EXCEPTION: %s\\n", e.what())' n1 = s.count(old) s = s.replace(old, new) print('fix1 (CRLF):', n1) old2 = 'EXCEPTION\r\n");' new2 = 'EXCEPTION\\n");' n2 = s.count(old2) s = s.replace(old2, new2) print('fix2 (CRLF):', n2) # also bare LF variants just in case old3 = 'EXCEPTION: %s\n", e.what())' new3 = 'EXCEPTION: %s\\n", e.what())' n3 = s.count(old3) s = s.replace(old3, new3) print('fix3 (LF):', n3) old4 = 'EXCEPTION\n");' new4 = 'EXCEPTION\\n");' n4 = s.count(old4) s = s.replace(old4, new4) print('fix4 (LF):', n4) io.open(p, 'w', encoding='utf-8', newline='').write(s) print('written')