python 重定向到文件

来源:互联网 发布:湖南大学校园网域名 编辑:程序博客网 时间:2024/05/01 16:40
#coding:utf-8import sysdef out():    console = sys.stdout #得到当前输出方向, 也就是控制台    file = open("data.txt", 'w')    sys.stdout = file #重定向到文件    print 'hello\n'+'java\n'+'python' #输出到文件    sys.stdout = console #又回到控制台    print 33 #在控制台打印33def in_():    file = open("data.txt", 'r')    sys.stdin = file    list = sys.stdin    for line in list:#获取到一行一行        print line[:-1] #去掉\nif __name__ == '__main__':    out()    in_()
原创粉丝点击