def sed_inplace(filename, pattern, repl):
    """
    Perform the pure-Python equivalent of in-place `sed` substitution: e.g.,
    `sed -i -e 's/'${pattern}'/'${repl}' "${filename}"`.
    """
pattern_compiled = re.compile(pattern)
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file:
    with open(filename) as src_file:
        for line in src_file:
            tmp_file.write(pattern_compiled.sub(repl, line))
shutil.copystat(filename, tmp_file.name)
shutil.move(tmp_file.name, filename)
sed_inplace('/etc/apt/sources.list', '^\\# deb', 'deb')
Как сделать sed как текст заменить на python?