开发者

save text file and display its content on the browser

If the following script.py writes "some text here" to output.txt file, my URL will be http://my_name/script.py. My question is, how can I read the output.txt as soon as (right after) the following function creates it, so that my URL reads like http://my_name/output.tx开发者_如何学JAVAt.

Many thanks in advance.

#------ script.py -------
def write_txt(){
   f=('./output.txt', 'w')
   f.write("some text here")
}


try webbrowser lib.

import webbrowser

myurl = "file:///mydir/output.txt"
webbrowser.open(myurl) 

However:

Note that on some platforms, trying to open a filename using this function, may work and start the operating system’s associated program.

That is: your file will probably be open in your default text editor (p.e. notepad). A possible solution is to give a custom extension to your file (p.e. output.url) and to associate the extension to your browser (not tested)


Depends on various factors, like OS and webserver used.

Pipe the output to the browser specifying a correct content-type, or, given you script writes to an accessible location, issue a HTTP redirect code pointing to that location.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜