0

I want to write the output of a shell command in a file using python. When I'm trying to do this the command is being run in the shell itself and outputting to the shell itself. Can I suppress the output of call and write it to a file?

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)

I tried subprocess and os methods but they didn't work. Still I am seeing command output in shell and nothing is being written the file ? Can you tell what am I missing ?

Community
  • 1
  • 1
Coderaemon
  • 3,619
  • 6
  • 27
  • 50
  • This post is not in python but its exactly what you're looking for: http://askubuntu.com/questions/522640/how-to-make-bin-ps-called-with-aux-as-its-argument-write-to-a-file/522960#522960 – Noitidart May 25 '15 at 05:50
  • 1
    This worked : http://stackoverflow.com/questions/14129251/redirect-output-to-a-text-file-using-windows-shell-in-python – Coderaemon May 25 '15 at 05:57
  • [look at the 2nd answer in the question linked by CoryKramer](http://stackoverflow.com/a/6482200/4279) – jfs May 25 '15 at 06:59

0 Answers0