FileModifiers
- FileModifiers.write_file_with_replacements(filename, replacements, full_overwrite=False)
Modify an existing file with replacement values.
This function writes a modified version of an existing file with replacements drawn from a user-provided dictionary. Candidates for replacement are identified by the keyname provided in the dictionary, where the new value is stored in
replacements[keyname]. It is written to identify variable assignments in the original file only, for example:double t_final = 50.0;
would be the intended target for
{"t_final": 25.0}to halve the variable value. It will preserve the assignment operator for “=”, “:”, and “ ” characters, where the last pattern represents any number of blank spaces.- Parameters:
filename (str) – The existing file to modify.
replacements (str) – A dictionary of replacement values where the keyname exactly matches the value of a variable created with an assignment operator.
full_overwrite (bool, optional) – An optional argument to specify that the value associated with
replacements[keyname]should be used to completely overwrite the matching line in the target file, i.e., it will not attempt to preserve any assignment operators, it will simply overwrite the target line with the value from the dictionary with no error checking. Defaults toFalse.
- Returns:
None