关于python的输出重定向

关于python的输出重定向,第1张

import sys

f = open('a.txt','w')

print >>sys.stdout,'hello,world'

hello,world

print >>f,'hello,world'

f.close()

输出到屏幕的内容重定向到文件,供参考。

另,print函数源码

def print(stream):

  """ print(value, ..., sep=' ', end='\\n', file=sys.stdout)

  

  Prints the values to a stream, or to sys.stdout by default.

  Optional keyword arguments:

  file: a file-like object (stream) defaults to 缺拍或the current sys.stdout.

  sep:  string inserted between values, default a space.

  end:  string appended after the last value, default 伏伍a newline. ""贺雀"

  pass

#!/usr/bin/env python

#coding=utf-8

from subprocess import Popen, PIPE

import re

def disk_space(pattern="核氏2[0-9]%", message="颤纤CAPACITY WARNING"):

## take shell command output

ps = Popen("df -h", shell=True, stdout=PIPE, stderr=PIPE)

output_lines = ps.stdout.readlines()

for line in output_lines:

line = line.strip()

if re.search(pattern, line):

print "%s %s" %(message, line)

if __name__ == '__main__'茄氏仿:

disk_space()


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/12286115.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存