[New-bugs-announce] [issue42279] replacements function corrupts file

John Dutcher report at bugs.python.org
Fri Nov 6 13:37:29 EST 2020


New submission from John Dutcher <jfdutcher1958 at yahoo.com>:

If the file below 0f 239 records is processed by the code below it throws no errors and writes the output...but the output has 615 records instead of 239 seemingly having written any revised ones three times instead of only once.

replacements = {'/1':'/01', '9/':'09/', '7/':'07/'}
file2 = open(r"c:\users\liddvdp\desktop\IBC CAP OUT.txt", "w")
with open(r"c:\users\liddvdp\desktop\IBC CAP.txt", "r") as reader:
         for line in reader:
             for src, target in replacements.items():
                 line = line.replace(src, target)
                 file2.write(line)

----------
assignee: terry.reedy
components: IDLE
files: IBC CAP.txt
messages: 380466
nosy: dutch58, terry.reedy
priority: normal
severity: normal
status: open
title: replacements function corrupts file
type: behavior
versions: Python 3.6
Added file: https://bugs.python.org/file49576/IBC CAP.txt

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42279>
_______________________________________


More information about the New-bugs-announce mailing list