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

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要好许多。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-19
  • 2022-02-18
  • 2022-02-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-11
  • 2021-11-21
  • 2021-12-11
  • 2021-06-09
  • 2021-07-11
  • 2022-12-23
相关资源
相似解决方案