假设有这么一段python3程序:

import sys
print ("")
sys.stderr.write("stderr1 ")

现在想要把程序的输出以及错误信息都存在文件中。

一开始是这么做的:

python ./test.py >& > t1

结果终端依然输出了信息

stderr1

而文件t1里面的内容是

[ss@localhost test]$ cat t1

搜索之后发现,shell解析重定向是从左往右按顺序解析。也就是先解析2>&1把stderr重定向到stdout也就是输出到终端,接着解析>t1把stdout重定向到t1文件。

所以为了达成目标,需要改成

python ./test.py > t1 >& 

这样就会先把stdout重定向,再重定向stderr,都输出到t1文件。

转载介绍:

文件描述符(file descriptor)
名称 类型 文件描述符 操作
标准输入 standard input <,<<
标准输出 standard output >,>>
标准错误输出 standard error output >,>>
文件描述符的存储位置位于 /proc/self/fd ,文件描述符是通过一系列软链接指向的默认输出设备,这里我们的默认设备就是模拟终端

https://www.cnblogs.com/divent/p/5773861.html

----------------

python ./test.py >& | cat >t1

似乎pipe只会接收stdout的输入,参考解释:

Note that the sequence of I/O redirections is interpreted left-to-right, but pipes are set up before the I/O redirections are interpreted. 
File descriptors such as and are references to open file descriptions. The operation >& makes file descriptor aka stderr refer to the same open
file description as file descriptor aka stdout is currently referring to (see dup2() and open()).
The operation >/dev/null then changes file descriptor so that it refers to an open file description for /dev/null,
but that doesn't change the fact that file descriptor 2 refers to the open file description which file descriptor 1 was originally pointing to — namely, the pipe.

如果是:

python ./test.py | cat >t1

那么t1中只会有stdout的输出。也可以尝试:

python ./test.py >& >/dev/null | cat >t1

只接收stderr输出。

import sys
print ("")
sys.stdout.flush()
sys.stderr.write("stderr1 ")
print ("")
sys.stdout.flush()

测试stderr是否有缓冲,结果显示似乎也是有缓冲的(行缓冲)。

python3中stderr重定向到文件时,似乎是全缓冲的

"python3": sys.stderr is line buffered at both the TextIOWrapper layer and may be fully buffered at the binary BufferedWriter layer if the output is redirected to a file

https://bugs.python.org/issue13601

#!/usr/bin/python
import sys
print("stdout1",end='')
sys.stderr.write("stderr1 ")
print("stdout2 ",end='')
sys.stderr.write("stderr2 ")
sys.stderr.flush()
print ("")
#sys.stdout.flush()
sys.stderr.write("error\n")

终端直接运行输出:

stderr1 stderr2 stdout1 stdout2 123
error

缓存模式和https://www.cnblogs.com/starRebel/p/8359455.html相似,需要注意的是python中的print默认带有换行符。

linuxI/O重定向的更多相关文章

  1. Linux-I/O重定向和管道

    Linux I/O重定向 标准输入(stdin):文件描述符0 标准输入(stdout):文件描述符1 标准错误(stderr):文件描述符2 file descriptors(FD,文件描述符 或 ...

  2. Http状态码之:301、302重定向

    概念 301 Moved Permanently 被请求的资源已永久移动到新位置,并且将来任何对此资源的引用都应该使用本响应返回的若干个URI之一.如果可能,拥有链接编辑功能的客户端应当自动把请求的地 ...

  3. 前端学HTTP之重定向和负载均衡

    前面的话 HTTP并不是独自运行在网上的.很多协议都会在HTTP报文的传输过程中对其数据进行管理.HTTP只关心旅程的端点(发送者和接收者),但在包含有镜像服务器.Web代理和缓存的网络世界中,HTT ...

  4. Web安全相关(三):开放重定向(Open Redirection)

    简介 那些通过请求(如查询字符串和表单数据)指定重定向URL的Web程序可能会被篡改,而把用户重定向到外部的恶意URL.这种篡改就被称为开发重定向攻击.   场景分析 假设有一个正规网站http:// ...

  5. Spring MVC重定向和转发以及异常处理

    SpringMVC核心技术---转发和重定向 当处理器对请求处理完毕后,向其他资源进行跳转时,有两种跳转方式:请求转发与重定向.而根据要跳转的资源类型,又可分为两类:跳转到页面与跳转到其他处理器.对于 ...

  6. Chrome在302重定向的时候对原请求产生2次请求的问题说明

    这个问题应该确确实实是一个Chrome的BUG,我在自己的编程环境中发现,并在多个服务器,多个编程语言的运行环境,以及多个浏览器下都测试过,都看到有2次请求出现.为了证明不是自己环境的问题,我也特意去 ...

  7. 重定向Http status code 303 和 302

    http 302 http 303 Http 302 302是一个普通的重定向代码.直观的看来是,请求者(浏览器或者模拟http请求)发起一个请求,然后服务端重定向到另一个地址.而事实上,服务端仅仅是 ...

  8. mvc 重定向的几种方式

    在RouteConfig添加一个简单的路由 //新增路由 routes.MapRoute( name: "Article", url: "Detial/{id}" ...

  9. C#开发微信门户及应用(14)-在微信菜单中采用重定向获取用户数据

    我曾经在系列文章中的<C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍>中介绍了微信菜单里面的重定向操作,通过这个重定向操作,我们可以获取一个code值,然后获取用户的open ...

随机推荐

  1. 【BZOJ2957】楼房重建 分块

    [BZOJ2957]楼房重建 Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子 ...

  2. 【BZOJ2251】[2010Beijing Wc]外星联络 后缀数组

    [BZOJ2251][2010Beijing Wc]外星联络 Description 小 P 在看过电影<超时空接触>(Contact)之后被深深的打动,决心致力于寻找外星人的事业.于是, ...

  3. CentOS7安装步骤详解

    准备环境 1.虚拟机  VMware Workstation 2.Centos7-64位安装包  ( CentOS-6.7-x86_64-bin-DVD1.iso ) 开始安装   进入安装初始化界面 ...

  4. 了解MIP(Mobile Instant Pages)

    mip官网:https://www.mipengine.org/   什么是mip? mip是百度在2016年提出的移动网页加速器项目.可以简单理解为是一个规范.   mip能做什么? mip能帮助站 ...

  5. 3.html+.ashx(删除学生信息)

    C03ListStu.ashx 0:false(删除);1:true(正常). (数据库里定义个BOOL型,TRUE表示正常FALSE表示删除) <html> <head> & ...

  6. ZOJ 3469Food Delivery(区间DP)

    Food Delivery Time Limit: 2 Seconds      Memory Limit: 65536 KB When we are focusing on solving prob ...

  7. Oracle管理监控之为11g asm磁盘组添加磁盘

    1.物理机挂在要添加的磁盘,虚拟机格式化虚拟硬盘 略 2.登录服务器:fdisk -l [root@node2 ~]# fdisk -l Disk /dev/sda: 107.3 GB, 107374 ...

  8. vue - nodejs

    一.知识 打开Nodejs英文网:https://nodejs.org/en/ 中文网:http://nodejs.cn/ 我们会发现这样一句话: 翻译成中文如下: Node.js 是一个基于 Chr ...

  9. 小米范工具系列之十:小米范SSH批量命令执行工具

    小米范SSH批量命令执行工具的主要功能是自动登录多台机器,并执行指定的命令,比如批量抓取shadow.批量获取系统版本.或者做基线时批量抓取配置等. 此工具使用java 1.8以上版本运行. 界面如下 ...

  10. 使用dockerfile 创建ubuntu ssh镜像

    ############################################################ # Dockerfile to build ubunto ssh contai ...