How to write and save html file in python?

前端 未结 6 2335
挽巷
挽巷 2020-12-05 02:22

This is what I know how to write and save it

Html_file= open\"(filename\",\"w\")
Html_file.write()
Html_file.close

But how do I save to the

6条回答
  •  半阙折子戏
    2020-12-05 02:56

    You can also do this without having to call close() using the with keyword. For example:

    # HTML String
    html = """
    
         <% for i in range(10): %>
           
    Number Square
    <%= i %> <%= i**2 %>
    """ # Write HTML String to file.html with open("file.html", "w") as file: file.write(html)

    See https://stackoverflow.com/a/11783672/2206251 for more details on the with keyword in Python.

提交回复
热议问题