读取文件的内容并在浏览器上显示

时间:2013-02-07 02:01:11

标签: python flask

我正在尝试用烧瓶来实现以下目的。

我的本​​地磁盘上有一个文件..它是一个大文件..所以我只想阅读该文件的前20行。

如何阅读文件并在浏览器上显示其内容?

任何指针..建议。 感谢

1 个答案:

答案 0 :(得分:1)

这样的事情应该有效

import webbrowser

# firstly you need to make write to an html file that will have the top 20 lines 
# you can do that using 

top_lines_file = open("shows.html", "w")
i = 1
while i in range(1,21):
    top_lines_file.write("write something to the file")
    i += 1
    # this will iterate over the file and write to it 20 times
top_lines_file.close() # close the file

# now you need to pass the path of the html file to the webbrowser object
webbrowser.open("file://" + path/to/the/html/file) 
# this will open the webbrowser with your html file