Reputation: 505
I'm writing a Python script for my thesis research, and I'm having problems with the subprocess module.
What the script does is executing "perf" with lots of args (here is a simplified version):
cmd = 'sudo perf stat -C %s -I %s -x , -e "%s"' % (core_perf, interval, perf_event_text)
Then I split cmd into args, to have tokens to send to subprocess:
final_cmd = cmd.split()
with open('output.txt', 'w') as outfile:
subprocess.Popen(final_cmd, stdout=outfile)
The problem is that this works well, but the output is shown through shell and not saved into file.
If I do it for "ls", it works like a charm:
with open('output.txt', 'w') as outfile:
subprocess.Popen("ls", stdout=outfile)
Any ideas?
Any help would be appreciated!
Thanks in advance!
Upvotes: 0
Views: 69
Reputation: 60604
another option is to pass the -o
parameter to perf
so it writes stderr output to the file you specify:
import shlex
import subprocess
cmd = shlex.split('perf stat -o outfile.txt ls')
subprocess.call(cmd)
Upvotes: 0
Reputation: 69042
perf stat
doesn't use stdout
for its output but stderr
, many programs starting a command and reporting on it (e.g. time
) do the same to not clobber the output of the executed command.
So in this case, instead of stdout=outfile
use stderr=outfile
.
Upvotes: 3