使用python将shell命令的输出写入文件?

时间:2015-05-21 14:22:40

标签: python shell subprocess

我想使用python在shell文件中编写shell命令的输出。当我尝试这样做时,命令正在shell本身运行并输出到shell本身。我可以抑制call的输出并将其写入文件吗?

from subprocess import call
import time

class PtsOutput(object):
        def __init__(self, camera, video_filename, pts_filename, pi_time_filename):
                self.camera = camera
                self.video_output = io.open(video_filename, 'wb')
                self.pi_time_output = io.open(pi_time_filename, 'w')
                self.start_time = None

        def write(self, buf):
                self.video_output.write(buf)
                if self.camera.frame.complete and self.camera.frame.timestamp:
                        if self.start_time is None:
                                self.start_time = self.camera.frame.timestamp
                        **starttime = call(["date","+%s%N"])**
                        self.pi_time_output.write('# frame start time: %s\n'%starttime)

我尝试了subprocess and os方法,但它们没有用。我仍然看到shell中的命令输出,没有写入文件?你能说出我错过了什么吗?

0 个答案:

没有答案