tags:

views:

40

answers:

1

In my code I have a line similar to this:

rval = subprocess.call(["mkdir",directoryName], shell=True)

and I can check rval to see if it is 0 or 1, but if it is 1, I would like to have the text from the command "A subdirectory or file ben already exists." in a file format, so I can compare it to another file if I want to make sure the text is the same.

Is it possible to have a line like this, but I know this does not work

rval = subprocess.call(["mkdir",directoryName], shell=True) >> filename

so no matter what happens with the command, the text is captured in filename, and rval still has the return code?

+1  A: 
import subprocess
f = open(r'c:\temp\temp.txt','w')
subprocess.call(['dir', r'c:\temp'], shell=True, stdout=f)
f.close()
Mark Ransom
Thank you, that works.
Dag
@Mark: Any reason why you used 'shell=True' in this case? Cause I read using that is a bad idea (on *nix at least).
sukhbir
@PulpFiction, two reasons: first it was included in the original question, second I was using a Windows shell command for testing.
Mark Ransom
@Mark: Thank you, just wanted to clarify :)
sukhbir