Question

Error message:

Failed to open output_file_path/**.txt

Code:

cmd = 'showTxt "%s" > "%s"' % (file_path, output_file_path)
LoggerInstance.log('[cmd] '+cmd)

#os.system(cmd)
splited_cmd=shlex.split(cmd)

p = subprocess.Popen(splited_cmd, stderr=subprocess.PIPE)
#p.wait()
output = p.stderr.read()
print output

LoggerInstance.log('[console std error]'+ output)

How to redirect stdout to a file in a cmd?

Was it helpful?

Solution

You can provide a file-handler as stdout parameter to Popen, i.e:

p = subprocess.Popen(splited_cmd,
                     stderr=subprocess.PIPE,
                     stdout=open(output_file_path, "w"))

Of course, be ready to catch the exception that it can throw.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top