how to change os.popen4 to subprocess

Discussion in 'Python' started by skyworld, Oct 27, 2012.

  1. skyworld

    skyworld Guest

    Hi,

    I'm new to python and I'm trying to porting some scripts from v0.96 to
    v2.0.1. A piece of code is like this:

    cmd_h = os.popen4(env['SYSCMDLINE'])[1]

    the system indicates the popen4 is deprecated and suggest to use
    subprocess. Can anybody tell me how to use subprocess in this case?
    and what does "[1]" here means?

    thanks.
     
    skyworld, Oct 27, 2012
    #1
    1. Advertising

  2. skyworld

    MRAB Guest

    On 2012-10-27 03:28, skyworld wrote:
    > Hi,
    >
    > I'm new to python and I'm trying to porting some scripts from v0.96 to
    > v2.0.1. A piece of code is like this:
    >
    > cmd_h = os.popen4(env['SYSCMDLINE'])[1]
    >
    > the system indicates the popen4 is deprecated and suggest to use
    > subprocess. Can anybody tell me how to use subprocess in this case?
    > and what does "[1]" here means?
    >

    os.popen4 returns a tuple of (child_stdin, child_stdout_and_stderr).
    The [1] gets the child_stdout_and_stderr member.

    Using the subprocess module:

    # Untested!
    cmd_h = subprocess.Popen(env['SYSCMDLINE'], stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT, shell=True).stdout

    Explanation:

    The command line: env['SYSCMDLINE']

    Return stdout: stdout=subprocess.PIPE

    stderr should be combined with stdout: stderr=subprocess.STDOUT

    Let the shell parse the command line: shell=True
     
    MRAB, Oct 27, 2012
    #2
    1. Advertising

  3. On 27/10/2012 03:28, skyworld wrote:
    > Hi,
    >
    > I'm new to python and I'm trying to porting some scripts from v0.96 to
    > v2.0.1. A piece of code is like this:


    What software are you talking about here, it's certainly not Python
    versions as the most up to date are 2.7.3 and 3.3.0?

    >
    > cmd_h = os.popen4(env['SYSCMDLINE'])[1]
    >
    > the system indicates the popen4 is deprecated and suggest to use
    > subprocess. Can anybody tell me how to use subprocess in this case?
    > and what does "[1]" here means?


    If you don't know what the [1] means you've got problems :) I suggest
    you read the tutorial here first
    http://docs.python.org/tutorial/index.html then the subprocess module
    here http://docs.python.org/library/subprocess.html#module-subprocess,
    specifically
    http://docs.python.org/library/subprocess.html#subprocess-replacements

    >
    > thanks.
    >


    No problem.

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Oct 27, 2012
    #3
  4. skyworld

    skyworld Guest

    On Oct 27, 11:02 am, MRAB <> wrote:
    > On 2012-10-27 03:28, skyworld wrote:> Hi,
    >
    > > I'm new to python and I'm trying to porting some scripts from v0.96 to
    > > v2.0.1. A piece of code is like this:

    >
    > > cmd_h = os.popen4(env['SYSCMDLINE'])[1]

    >
    > > the system indicates the popen4 is deprecated and suggest to use
    > > subprocess. Can anybody tell me how to use subprocess in this case?
    > > and what does "[1]" here means?

    >
    > os.popen4 returns a tuple of (child_stdin, child_stdout_and_stderr).
    > The [1] gets the child_stdout_and_stderr member.
    >
    > Using the subprocess module:
    >
    > # Untested!
    > cmd_h = subprocess.Popen(env['SYSCMDLINE'], stdout=subprocess.PIPE,
    > stderr=subprocess.STDOUT, shell=True).stdout
    >
    > Explanation:
    >
    > The command line: env['SYSCMDLINE']
    >
    > Return stdout: stdout=subprocess.PIPE
    >
    > stderr should be combined with stdout: stderr=subprocess.STDOUT
    >
    > Let the shell parse the command line: shell=True


    thanks
     
    skyworld, Oct 27, 2012
    #4
  5. Replying to skyworld because I could not find the original message
    from MRAB.

    skyworld wrote:

    > On Oct 27, 11:02 am, MRAB <> wrote:

    > > On 2012-10-27 03:28, skyworld wrote:> Hi,
    > >

    > > > I'm new to python and I'm trying to porting some scripts from v0.96 to
    > > > v2.0.1. A piece of code is like this:

    > >

    > > > cmd_h = os.popen4(env['SYSCMDLINE'])[1]

    > >

    > > > the system indicates the popen4 is deprecated andsuggest to use
    > > > subprocess. Can anybody tell me how to use subprocess in this case?
    > > > and what does "[1]" here means?

    > >
    > > os.popen4 returns a tuple of (child_stdin, child_stdout_and_stderr).
    > > The [1] gets the child_stdout_and_stderr member.
    > >
    > > Using the subprocess module:
    > >
    > > # Untested!
    > > cmd_h = subprocess.Popen(env['SYSCMDLINE'], stdout=subprocess.PIPE,
    > > stderr=subprocess.STDOUT, shell=True).stdout
    > >
    > > Explanation:
    > >
    > > The command line: env['SYSCMDLINE']
    > >
    > > Return stdout: stdout=subprocess.PIPE
    > >
    > > stderr should be combined with stdout: stderr=subprocess.STDOUT
    > >
    > > Let the shell parse the command line: shell=True

    >
    > thanks
    > --


    I thought the usage of shell=True is usually discouraged? The
    subprocess documentation[0] should be helpful to figure it out.
    """
    Warning: Invoking the system shell with shell=True can be a security
    hazard if combined with untrusted input. See the warning under
    Frequently Used Arguments for details.
    """

    [0] http://docs.python.org/2/library/subprocess.html


    Ramit


    This email is confidentialand subject to important disclaimers and
    conditions including on offers for the purchase or sale of
    securities, accuracy and completeness of information, viruses,
    confidentiality, legal privilege, and legal entity disclaimers,
    available at http://www.jpmorgan.com/pages/disclosures/email.
     
    Prasad, Ramit, Oct 30, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick L. Nolan
    Replies:
    0
    Views:
    356
    Patrick L. Nolan
    Dec 18, 2003
  2. Patrick L. Nolan

    Popen4 -> null lines on Windows

    Patrick L. Nolan, Dec 30, 2003, in forum: Python
    Replies:
    2
    Views:
    319
    Patrick L. Nolan
    Dec 30, 2003
  3. Roman Yakovenko
    Replies:
    0
    Views:
    1,632
    Roman Yakovenko
    Sep 7, 2004
  4. Jeff Epler
    Replies:
    0
    Views:
    420
    Jeff Epler
    Sep 8, 2004
  5. hiral
    Replies:
    2
    Views:
    633
    Jean-Michel Pichavant
    May 5, 2010
Loading...

Share This Page