Python的print输出重定向举例分析

发布时间:2021-11-19 15:04:55 作者:iii
来源:亿速云 阅读:239

本篇内容介绍了“Python的print输出重定向举例分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

Python中调试程序使用最多的是print(),在使用print()打印时事实上是调用了 sys.stdout.write()。不过print在把内容打印到控制台后,追加了一个换行符(linefeed)。以下例程中,print和sys.stdout.write()是等价的:

sys.stdout.write('Hello World\n')
print('Hello World')

在Python中, sys.stdin、sys.stdout和sys.stderr分别对应解释器的标准输入、标准输出和标准出错流。在程序启动时,这些对象的初值由sys.stdin、sys.__stdout__和sys.__stderr__保存,比便于恢复标准流对象。如下所示:

print(sys.stdout) # <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
print(sys.stdin) # <_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>
print(sys.stderr) # <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>
print(sys.__stdout__) # <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
print(sys.__stdin__) # <_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>
print(sys.__stderr__) # <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>

如果我们要把内容重定向到文本中去时,该如何操作呢?我们先看下普通的文本对象和标准输出对象的区别。如下所示:

print(dir(sys.stdout))
"""
['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', 
'__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', 
'__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', 
'__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
'__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', 
'_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 
'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 
'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 
'write_through', 'writelines']
"""
with open('redirect.txt', 'w') as f:
    print(f) # <_io.TextIOWrapper name='redirect.txt' mode='w' encoding='cp1252'>
    print(dir(f))
    """
    ['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', 
    '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__getstate__', 
    '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', 
    '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', 
    '__sizeof__', '__str__', '__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', 
    '_checkWritable', '_finalizing', 'buffer', 'close', 'closed', 'detach', 'encoding', 'errors', 
    'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 
    'readline', 'readlines', 'reconfigure', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 
    'write_through', 'writelines']
    """

可见两者都属于文件对象,其中所包含的方法也都相同,比如write、read等等。所以,如果把文件对象的引用赋值给sys.stdout,那么print调用的即为文件对象的write方法,这样就实现了重定向。其实在之前的Python基础教程中有跟大家讲古。代码如下所示:

with open('redirect.txt', 'w') as f:
    sys.stdout = f
    print("Hello World")

重定向后,print打印的内容就从控制台搬到了文本上了,如下所示:

Python的print输出重定向举例分析

如果只是临时向文件中打印内容,之后仍然会在控制台上打印的话,应该先将原始的控制台引用对象保存下来,之后将该引用恢复到sys.stdout中。如下所示:

__console__ = sys.stdout
# redirection start
# ...
# redirection end
sys.stdout = __console__

以上的实现方法并不优雅,典型的实现如下所示:

# 临时把标准输出重定向到一个文件,然后再恢复正常
with open('redirect.txt', 'w') as f:
    oldstdout = sys.stdout
    sys.stdout = f
    try:
        help(__import__)
    finally:
        sys.stdout = oldstdout
print("Hello World")

Python的print输出重定向举例分析

接下来介绍Pyhton上下文管理器redirect_stdout实现重定向的方法。contextlib.redirect_stdout在Python 3.4加入。如下所示:

with open('redirect.txt', 'w') as f:
    with contextlib.redirect_stdout(f):
        help(pow)

Python的print输出重定向举例分析

当然,其实redirect_stdout的内在实现逻辑也仅是保存控制台的引用,而后恢复如此而已。于是我们可以实现自己的redirect_stdout上下文管理器。如下所示:

@contextlib.contextmanager
def redirect_stdout(fileobj):
    oldstdout = sys.stdout
    sys.stdout = fileobj
    try:
        yield fileobj
    finally:
        sys.stdout = oldstdout
def redirect4():
    with open('redirect.txt', 'w') as f:
        with redirect_stdout(f):
            help(pow)
    print("Hello World")

“Python的print输出重定向举例分析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. Python语法举例分析
  2. Python的正则规则举例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python print

上一篇:Python的特性有哪些

下一篇:Python基本数据类型有什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》