LINUX.ORG.RU

История изменений

Исправление vvn_black, (текущая версия) :

Чем же )

from pathlib import Path

filename = 'config'
filename_bak = Path(filename).rename(Path(filename).with_suffix('.bak'))

with open(filename_bak, 'r') as fr, open(filename, 'w') as fw:
    for line in fr:
        if '*' in line:
            fw.write(line.replace('*', '#'))
        else:
            fw.write(line)

Upd. Или даже так, если вынести логику замены отдельно и это небольшой текстовик:

def change_line(line):
    if '*' in line:
        return line.replace('*', '#')

    return line

with open(filename_bak, 'r') as fr, open(filename, 'w') as fw:
    fw.write(''.join(map(change_line, fr)))

Исправление vvn_black, :

Чем же )

from pathlib import Path

filename = 'config'
filename_bak = Path(filename).rename(Path(filename).with_suffix('.bak'))

with open(filename_bak, 'r') as fr, open(filename, 'w') as fw:
    for line in fr:
        if '*' in line:
            fw.write(line.replace('*', '#'))
        else:
            fw.write(line)

Upd. Или даже так, вынести логику замены отдельно:

def change_line(line):
    if '*' in line:
        return line.replace('*', '#')

    return line

with open(filename_bak, 'r') as fr, open(filename, 'w') as fw:
    fw.write(''.join(map(change_line, fr)))

Исходная версия vvn_black, :

Чем же )

from pathlib import Path

filename = 'config'
filename_bak = Path(filename).rename(Path(filename).with_suffix('.bak'))

with open(filename_bak, 'r') as fr, open(filename, 'w') as fw:
    for line in fr:
        if '*' in line:
            fw.write(line.replace('*', '#'))
        else:
            fw.write(line)