【问题标题】:Trouble writing to html file with python [closed]使用python写入html文件时遇到问题[关闭]
【发布时间】:2016-08-12 17:51:18
【问题描述】:

我在用 python 编写一个非常简单的 html 文件时遇到了麻烦。

代码是:

filehtml = 'C:\pathhere\helloworld.html'

message = '''<html>
<head></head>
<body><p><Hello World!</p></body>
</html>'''

f = open(filehtml,'w')
f.write(message)
f.close()

该文件出现,大约 65 个字节,但是当我在 chrome 或 firefox 中打开它时,它是空白的。文件图标上有一个小锁,但是当我右键单击选项时,权限包括我。所以...我不知道这是代码问题还是其他问题。此外,我桌面上的所有其他文件都有锁,我可以很好地打开它们。 我在这里寻求帮助 (How to write and save html file in python?)

我使用的是 Windows 7,python 2.7 Anaconda 谢谢。

【问题讨论】:

  • 用记事本打开文件看是否正确写入
  • 顺便说一句,你应该使用with-syntax
  • 标记为关闭,因为这是一个查找我的错字问题
  • 我现在明白了,对不起!我读了很多遍,都是在喝咖啡之前。

标签: python html


【解决方案1】:

你有一个额外的&lt;,它把一切都搞砸了。

filehtml = 'C:\pathhere\helloworld.html'

message = '''<html>
<head></head>
<body><p>Hello World!</p></body>
</html>'''

f = open(filehtml,'w')
f.write(message)
f.close()

【讨论】:

    猜你喜欢
    • 2021-11-27
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 2014-08-22
    • 1970-01-01
    相关资源
    最近更新 更多