write Python string object to file

前端 未结 2 534
遇见更好的自我
遇见更好的自我 2021-01-28 16:33

I have this block of code that reliably creates a string object. I need to write that object to a file. I can print the contents of \'data\' but I can\'t figure out how to write

2条回答
  •  隐瞒了意图╮
    2021-01-28 16:49

    with open (template_file, "r") as a_string:
       data=a_string.read().replace('{SERVER_NAME}', server_name).replace('{BRAND}', brand).replace('{CONTENT_PATH}', content_path).replace('{DAMPATH}', dampath).replace('{ENV}', env).replace('{CACHE_DOCROOT}', cache_docroot).replace('{SLD}', sld)
    
    filename = "{NNN}_{BRAND}_farm.any".format(BRAND=brand, NNN=nnn)
    with open(filename, "w") as outstream:
        outstream.write(data)
    

提交回复
热议问题