jiabanl 发表于 2017-5-4 08:10:17

在python脚本中重定向至系统标准输出

  一般在linux系统中,为了方便起见,我们都不会做太复杂的命令行参数来交互,而是直接将输出重定向到系统标准输出,这样可以方便使用管道等来保存输出结果。 在python脚本中也可以实现这样的功能:

import sys

#open file ; redirect to sys.stderr
try:
myf = open(sys.argv,'r')
except IndexError:
print >>sys.stderr, 'your info(maybe this is USAGE info)'#redirect
sys.exit(1)
# output
for line in myf:
print >>sys.stdout, '\t'.join(line.split())#redirect

myf.close()
 
   为什么不直接使用print ??? 因为那样不一定能获得输出结果吧!详情可参考python文档!
  GOOD LUCK!
页: [1]
查看完整版本: 在python脚本中重定向至系统标准输出