pwtools.common.file_template_replace

pwtools.common.file_template_replace(fn, dct, bak='', **kwargs)[source]

Replace placeholders in file fn.

Parameters:
  • fn (str) – Filename

  • dct (dict) – Replacement rules

  • bak (str) – ‘’ : no backup is done ‘<str>’ : fn is backed up to “fn<str>”

  • kwargs (kwargs to template_replace())

Examples

dct = {‘xxx’: ‘foo’, ‘yyy’: ‘bar’} fn = ‘bla.txt’ file_template_replace(fn, dct, ‘.bak’, mode=’txt’)

This the same as: shell$ sed -i.bak -r -e ‘s/xxx/foo/g -e ‘s/yyy/bar/g’ bla.txt