python 将输出写入文件

加入以下模式,就可将输出写入文件

str = 'a'
f = open("result.txt","w")
f.write(str)

以上示例将a写入了result.txt中。

将输出写入文件,在需要看长片段的输出时很管用,例如,

#-*-coding:utf-8-*-
"""
my first App
"""
import sys
import urllib
def Main():
    htmlresult=urllib.urlopen("http://www.baidu.com").read()
    #print htmlresult
    fo.write(htmlresult)
#this is test
if __name__=="__main__":
    fo = open("result.txt","w")
    Main()

比直接print要好许多。

猜你喜欢

转载自www.cnblogs.com/artesian0526/p/9290141.html