writing terminal output to file

拥有回忆 提交于 2019-11-29 12:29:55

I believe this does what you want. Argument to os.system() should be a string representing command to the OS.

os.system('pdv -t %s > 123.txt' % epoch_name)

There is subprocess module, which may worth look into if you are planning to process the output further in python.

You can use subprocess.call, with the stdout keyword argument:

import subprocess

cmd = ['ls', '-l']

with open('output.txt', 'w') as out:
    return_code = subprocess.call(cmd, stdout=out)

Its better to use subprocess module that os.system

import subprocess
subprocess.call(['pdv', '-t', filename, '>', dest_file_name])
from subprocess import Popen
proc = Popen(['pdv', '-t', epoch_name], stdout = open('123.txt', 'w'))

See if the command script is available to you. It might do what you need.

Or you can use sys.stdout check this document from DiveIntoPython or the discussion here if it helps you out.

import sys

print 'Dive in'
saveout = sys.stdout
fsock = open('out.log', 'w')
sys.stdout = fsock
print 'This message will be logged instead of displayed'
sys.stdout = saveout
fsock.close()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!