Generate config file from template using Python search and replace.
Peter Otten
__peter__ at web.de
Tue Dec 1 10:43:33 EST 2015
Mr Zaug wrote:
> That makes sense.
>
> So I still can't see how to write the string object to a file whist naming
> the file with whatever values I provided for the NNN and BRAND variables.
>
> Printing the contents of the string object is working with all the
> expected substitutions. Do I need to convert the string object into a file
> before I write it? Or do I need to open a new file and somehow stuff the
> string object into it?
Yes, open a new file and write the string using the write() method. You can
use string formatting to build the filename, too:
text = ... # the string you want to write
nnn = raw_input("What is the serial number of the site? ")
brand = raw_input("What is the brand, or product name? ")
filename = "{NNN}{BRAND}_farm.any".format(BRAND=brand, NNN=nnn)
with open(filename, "w") as outstream:
outstream.write(text)
More information about the Python-list
mailing list