Python重定向

1.标准输入和输出

01.sys
 stderr = None # (!) real value is "<_io.TextIOWrapper name='<stderr>' mode='w' encoding='gbk'>"
 stdin = None  # (!) real value is "<_io.TextIOWrapper name=3 mode='r' encoding='cp936'>"
 stdout = None # (!) forward: __stdout__, 
                     real value is "<_io.TextIOWrapper name='<stdout>' mode='w' encoding='gbk'>"
 __stderr__ = stderr
 __stdin__ = None # (!) real value is "<_io.TextIOWrapper name='<stdin>' mode='r' encoding='gbk'>"
 __stdout__ = None # (!) real value is "<_io.TextIOWrapper name='<stdout>' mode='w' encoding='gbk'>"
 
 argv = [] # real value of type <class 'list'> skipped
02. 
 Built-in functions, exceptions, and other objects.
 def print(self, *args, sep=' ', end='
', file=None): # known special case of print
 
 # redirect stdout     
 
     sys.stdout.write(obj+'
')

2.信号与快捷键

Linux中
	ctrl-c 是发送 SIGINT  信号。终止一个进程 
	      ctrl-c: ( kill foreground process ) 发送 SIGINT 信号给前台进程组中的所有进程,强制终止程序的执行
		  ctrl + c停止将停止整个过程
	ctrl-z 是发送 SIGSTOP 信号,挂起一个进程;命令fg唤回进程。
         ctrl-z: ( suspend foreground process ) 发送 SIGTSTP 信号给前台进程组中的所有进程,
		 常用于挂起一个进程,而并非结束进程	
	ctrl-d 不是发送信号,一般表示 EOF,ctrl-d: ( Terminate input, or exit shell ) 
	        一个特殊的二进制值,表示 EOF,作用相当于在终端中输入exit后回车
# stty命令 修改终端命令行的相关设置
    :~$ stty -a 
      intr = ^C; susp = ^Z; eof = ^D; quit = ^; erase = ^H; kill = ^U; 
      start = ^Q; stop = ^S; rprnt = ^R; werase = ^W; lnext = ^V;
      discard = ^O;  
	  
	ctrl-s   中断控制台输出  ctrl-q   恢复控制台输出

3.输入和输出

 类文件对象(file-like object),简单说就是类似文件对象的对象,至少要具备read()和write()两个方法
  Python中,通常使用io模块实现类文件对象。
   该模块提供了文本和二进制两种缓冲区,分别对应文本型和二进制型的类文件对象
    from io import StringIO, BytesIO
    tfo = StringIO() # 创建一个类文本文件对象
    bfo = BytesIO() # 创建一个类二进制文件对象

4.重定向

 01. linux重定向 shell
 	 std_.py < input.txt
    >
    |  管道
  Linux 下也可以通过 shell 的输出重定向,将 print 的内容写入文件:
   python3 your_script.py >> log.txt
Python重定向
   Python 使用任意实现了 write 方法的对象作为标准输出对象
stdout就像是一个类文件对象, 可以将他赋值给任意的一个文件对象,重定向输出
  
代码
import sys
print("123")
__console = sys.stdout   # 用于后期还原
# 把打印重定向文件
f=open('outfile.log',"a+") 
sys.stdout=f
print('in outfile')

01.重定向-交互式示例

交互式的python的重定向
 python
 import sys
 message = sys.stdin.readline() 			
 Hello
 World
 
 光标就停在那了,Enter 结束输入
 message = sys.stdin.readlines() 
 则结束键
 linux   系统结束输入的方法  Ctrl + d+ Enter
 windows 系统结束输入的方法  Ctrl+z 然后按 Enter

02.重定向示例-代码

1.代码示例
 import sys

 class Output:
     def __init__(self):
         self.text = ''
 
     def write(self, line):
         self.text += line
 
     def writelines(self, lines):
         [self.write(line) for line in lines]
 
     def flush(self):
         self.text = ''
		 
 class Input:
     def __init__(self, input):
         self.text = input
 
     def read(self):
         return self.text
 
     def readline(self):
         lines = self.text.find('
')
         if lines == -1:
             return self.text
         else:
             return self.text[:lines+1]
 # 重定向,输入
 savesteams = sys.stdin, sys.stdout
 
2.sys.stderr, sys.stdin 也都可以被重定向到多个地址
  重定向到的对象实现一个 write 方法
  
class __redirection__:  
    def __init__(self):
        self.buff=''
        self.__console__=sys.stdout   
    def write(self, output_stream):
        self.buff+=output_stream
        
    def to_console(self):
        sys.stdout=self.__console__
        print self.buff
    def to_file(self, file_path):
        f=open(file_path,'w')
        sys.stdout=f
        print self.buff
        f.close()
    
    def flush(self):
        self.buff=''
        
    def reset(self):
        sys.stdout=self.__console__
    
r_obj=__redirection__()
sys.stdout=r_obj
# redirect to console
r_obj.to_console()
# redirect to file
r_obj.to_file('out.log'

参考

  Python 标准输出 sys.stdout 重定向 https://www.cnblogs.com/turtle-fly/p/3280519.html
  深入理解Python sys.stdin sys.stdout 玩转命令行  深入理解Python sys.stdin sys.stdout 玩转命令行
原文地址:https://www.cnblogs.com/ytwang/p/15119771.html