使用Python创建一个空的HTML文件?

时间:2013-12-08 11:22:27

标签: python html operating-system

我需要使用python创建一个空的html文件。我是OS模块的新手,我找不到任何这样做的例子。

如何使用python创建一个空的html文件?

谢谢!

3 个答案:

答案 0 :(得分:4)

您不需要os模块;只需打开一个文件进行编写并关闭它:

with open('index.html', 'w'):
    pass

您可能想要明确地写任何内容,因为如果您只是打开一个文件而不对其执行任何操作,其他开发人员可能会感到困惑:

with open('index.html', 'w') as f:
    f.write('')

答案 1 :(得分:1)

这只是创建一个扩展名为.html的空文本文件。

您可以使用以下命令在Python中执行此操作:

file = open('emptyfile.html', 'w')  
file.close()

答案 2 :(得分:1)

with open('example.html', 'w') as f:
    # you can do something with f
    pass