[Baypiggies] how can I change the input parameter in my executable while calling from either os.system or os.popen calls?

Joshua Gallagher joshua.gallagher at gmail.com
Thu Aug 21 19:01:20 CEST 2008


There may be another, and better, way of doing it, but I usually use
Popen like this:
cmd = <something>
p_obj = subprocess.Popen(cmd, shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
output = p_obj.stdout.read()

In your example, it would look something like this:
import subprocess



class reg:

    def __init__ (self, offset):

        self.offset = offset



    def reg_read(self):

        print 'Read the reg content specified by offset'

        read_cmd = './testtool  %s' %self.offset

        p_obj = subprocess.Popen(read_cmd, shell=True,

                                 stdout=subprocess.PIPE,

                                 stderr=subprocess.STDOUT)

        output = p_obj.stdout.read()



reg1 = reg(0x50)

reg1.reg_read()

I'd be happy to hear of a better way to do this.

Joshua



On Thu, Aug 21, 2008 at 9:30 AM, David Elsen <elsen.david08 at gmail.com> wrote:
> Shannon,
> It is a very big reference for a very small question. I tried to glance and
> could not get my solution.
>
> Thanks a lot for the reference though,
> David
>
> On Wed, Aug 20, 2008 at 5:59 PM, Shannon -jj Behrens <jjinux at gmail.com>
> wrote:
>>
>> On Wed, Aug 20, 2008 at 5:53 PM, David Elsen <elsen.david08 at gmail.com>
>> wrote:
>> > Hello all,
>> >
>> > I have defined a class as following:
>> >
>> > class reg:
>> >      def __init__ (self, offset):
>> >                 self.offset = offset
>> >
>> >      def reg_read(self, offset):
>> >              print 'Read the reg conten specified by offset'
>> >              import os
>> >              read_cmd = './testtool  0x50'
>> >              read_content = os.popen(read_cmd).read()
>> >
>> > reg1 = reg (0x50)
>> >
>> > reg1.reg_read(0x50)
>> >
>> >
>> > The above code calls my c executable with offset 0x50 and gives me the
>> > content of 0x50 offset.
>> >
>> > I wanted to change the above code as following:
>> >
>> > class reg:
>> >      def __init__ (self, offset):
>> >                 self.offset = offset
>> >
>> >      def reg_read(self, offset):
>> >              print 'Read the reg conten specified by offset'
>> >              import os
>> >              read_cmd = './testtool  offset'
>> >              read_content = os.popen(read_cmd).read()
>> >
>> > reg1 = reg (0x50)
>> >
>> > reg1.reg_read(0x50)
>> >
>> > Basically I wanted to pass the offset as input parameter to my
>> > executable
>> > "testtool" and run the testtool for offset.
>> >
>> > But looks like it does not allow me to change my offset.
>> >
>> > Is there a way to do this in python?
>> >
>> > Thanks in advance,
>> > David
>>
>> Have a peek at http://docs.python.org/lib/module-subprocess.html.
>> This has the additional benefit that you can pass a list of args
>> instead of a single string.
>>
>> -jj
>>
>> --
>> Don't you wish you had a really clever sig like mine?
>> http://jjinux.blogspot.com/
>
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>


More information about the Baypiggies mailing list