刚开始学习python。试图理解为什么html代码的内容不会出现在创建的文件中。它只是空白。
message = """<html>
<head></head>
<body><p>Hello World!</p></body>
</html>"""
my_html_file = open("/Users/Negus/Desktop/hello.html", "w")
my_html_file.write(message)
答案 0 :(得分:1)
您必须关闭文件:
message = """<html>
<head></head>
<body><p>Hello World!</p></body>
</html>"""
my_html_file = open("/Users/Negus/Desktop/hello.html", "w")
my_html_file.write(message)
my_html_file.close()
或使用此代码:
with open("/Users/Negus/Desktop/hello.html", "w") as my_html_file:
my_html_file.write(message)