print重定向

# -*- coding: utf-8 -*-
'''
@File  : test.py
@Author: qiguagnhui
@Date  : 2020-04-11 23:17
@Desc  :
'''
import os
import sys


savedStdout = sys.stdout  #保存标准输出流

with open('out.txt', 'w') as file:
    sys.stdout = file  #标准输出重定向至文件
    content = os.popen('pwd', 'r').read()
    print(content)
    print('000,打印的方向已经被调整到了文本中,这会输出在文本中,不在控制台')

sys.stdout = savedStdout  #恢复标准输出流
print('111, 恢复打印的方向,这会输出在控制台,不会在文本中')
posted @ 2020-04-16 23:52  疯狂列表推导式  阅读(135)  评论(0编辑  收藏  举报