Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32260 > unrolled thread

how to change os.popen4 to subprocess

Started byskyworld <chenyong20000@gmail.com>
First post2012-10-26 19:28 -0700
Last post2012-10-27 04:10 +0100
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  how to change os.popen4 to subprocess skyworld <chenyong20000@gmail.com> - 2012-10-26 19:28 -0700
    Re: how to change os.popen4 to subprocess MRAB <python@mrabarnett.plus.com> - 2012-10-27 04:02 +0100
      Re: how to change os.popen4 to subprocess skyworld <chenyong20000@gmail.com> - 2012-10-26 23:30 -0700
        RE: how to change os.popen4 to subprocess "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-30 14:47 +0000
    Re: how to change os.popen4 to subprocess Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-27 04:10 +0100

#32260 — how to change os.popen4 to subprocess

Fromskyworld <chenyong20000@gmail.com>
Date2012-10-26 19:28 -0700
Subjecthow to change os.popen4 to subprocess
Message-ID<c6bac1cc-abda-4d95-b3b4-e96dc7ab7a3a@r8g2000pbs.googlegroups.com>
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.

[toc] | [next] | [standalone]


#32262

FromMRAB <python@mrabarnett.plus.com>
Date2012-10-27 04:02 +0100
Message-ID<mailman.2928.1351306928.27098.python-list@python.org>
In reply to#32260
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

[toc] | [prev] | [next] | [standalone]


#32267

Fromskyworld <chenyong20000@gmail.com>
Date2012-10-26 23:30 -0700
Message-ID<ea712e7b-bad9-4763-bbae-22a824f07256@qi8g2000pbb.googlegroups.com>
In reply to#32262
On Oct 27, 11:02 am, MRAB <pyt...@mrabarnett.plus.com> 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

[toc] | [prev] | [next] | [standalone]


#32494

From"Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Date2012-10-30 14:47 +0000
Message-ID<mailman.3093.1351608486.27098.python-list@python.org>
In reply to#32267
Replying to skyworld because I could not find the original message
from MRAB.

skyworld wrote:
> On Oct 27, 11:02 am, MRAB <pyt...@mrabarnett.plus.com> 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
> --

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 confidential and 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.  

[toc] | [prev] | [next] | [standalone]


#32263

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-10-27 04:10 +0100
Message-ID<mailman.2929.1351307261.27098.python-list@python.org>
In reply to#32260
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web