[Answer]-Django project not writing to file

1👍

The following will replace the contents of the file:

with open('/home/me/dev/Django/rulebase/result.rb', 'r') as discover_reading:
    lines = [line.replace('input = "', 'this') for line in discover_reading.readlines()]

with open('/home/me/dev/Django/rulebase/result.rb', 'w') as discover_reading:
    discover_reading.writelines(lines)

You’re example does not work at all.

Read the docs for file objects and string.replace please. File is not a builtin function, str.replace does not replace in-place (strings are immutable) and the mydiscover.closed is for telling whether or not the file is closed, not for closing an open file.

Your original error was raised because you are attempting to read lines from a file you’ve opened with the a flag which stands for append.

0👍

You must open it using ‘r’ flag, for reading.

open('/home/me/dev/Django/rulebase/result.rb', 'r')
👤caio

Leave a comment