Win XP: Problem with shell scripting in Python

A.M alanalan at newsgroup.nospam
Fri Jun 9 13:00:16 EDT 2006



Here is what I came up with after John and Fredrik's help.



import os

import sys



def Execute(shell_command,logStream = sys.stdout):

    print >>logStream, shell_command

    child_stdin, child_stdout_and_stderr  = os.popen4(shell_command)

    commad_output = child_stdout_and_stderr.read()

    print >>logStream,  commad_output

    return_code = child_stdout_and_stderr.close()

    return_code = return_code or child_stdin.close()

    print  >>logStream, "Return Code: " , return_code



Execute ("DIR")

Execute ("MD :")



I tested it and so far it behaves the way that I want.



The tricky part is that when you use popen4, you have to close both returned 
streams to be able to get the return code. I wasn't able to find that in the 
documentation.



Alan







More information about the Python-list mailing list