473,763 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Output from subprocess.Pope n()

Output from the shell:

[cpn@s0 teste]$ set | grep IFS
IFS=$' \t\n'

Output from subprocess.Pope n():
>>import subprocess as sub
p = sub.Popen('set | grep IFS', shell=True, stdout=sub.PIPE )
p.stdout.read lines()[1]
"IFS=' \t\n"

Both outputs for comparison:
IFS=$' \t\n'
"IFS=' \t\n"

The subprocess.Pope n() output is missing the $ and the last '

How to get the raw shell output from subprocess.Pope n()?

Regards, Clodoaldo Pinto Neto

Oct 15 '06 #1
9 9378
Clodoaldo Pinto Neto wrote:
Output from the shell:

[cpn@s0 teste]$ set | grep IFS
IFS=$' \t\n'

Output from subprocess.Pope n():
>>>import subprocess as sub
p = sub.Popen('set | grep IFS', shell=True, stdout=sub.PIPE )
p.stdout.rea dlines()[1]
"IFS=' \t\n"

Both outputs for comparison:stdo ut=subprocess.P IPE)
IFS=$' \t\n'
"IFS=' \t\n"

The subprocess.Pope n() output is missing the $ and the last '
if you expect one line of output, why are you doing readlines()[1] ?
>>f = subprocess.Pope n("set | grep IFS", shell=True,
f.stdout.read lines()[1]
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range

this works for me:
>>f = subprocess.Pope n("set | grep IFS", shell=True,
stdout=subproce ss.PIPE)
>>f.stdout.read lines()
["IFS=$' \\t\\n'\n"]

what does the above return on your machine?

</F>

Oct 15 '06 #2

Fredrik Lundh wrote:
this works for me:
>>f = subprocess.Pope n("set | grep IFS", shell=True,
stdout=subproce ss.PIPE)
>>f.stdout.read lines()
["IFS=$' \\t\\n'\n"]

what does the above return on your machine?
>>f = subprocess.Pope n("set | grep IFS", shell=True, stdout=subproce ss.PIPE)
f.stdout.read lines()
["BASH_EXECUTION _STRING='set | grep IFS'\n", "IFS=' \t\n"]

I'm on FC5

Oct 15 '06 #3
Clodoaldo Pinto Neto <cl************ *@gmail.comwrot e:
>Fredrik Lundh wrote:
>this works for me:
> >>f = subprocess.Pope n("set | grep IFS", shell=True,
stdout=subproc ess.PIPE)
> >>f.stdout.read lines()
["IFS=$' \\t\\n'\n"]

what does the above return on your machine?
>>>f = subprocess.Pope n("set | grep IFS", shell=True, stdout=subproce ss.PIPE)
f.stdout.rea dlines()
["BASH_EXECUTION _STRING='set | grep IFS'\n", "IFS=' \t\n"]

I'm on FC5
OK, there's something going on here:
$ set | grep IFS
IFS=$' \t\n'
$ python
Python 2.4.1 (#2, May 5 2005, 11:32:06)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2
Type "help", "copyright" , "credits" or "license" for more information.
>>import subprocess
f = subprocess.Pope n("set | grep IFS", shell=True, stdout=subproce ss.PIPE)
f.stdout.read lines()
["IFS=' \t\n"]
>>>
(Hmph, I don't have any other non-Windows boxes with >=2.4 to hand.)

--
\S -- si***@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Oct 16 '06 #4
Now we have 3 different outputs from 3 people to the command:
>>f = subprocess.Pope n("set | grep IFS", shell=True,
stdout=subproce ss.PIPE)
>>f.stdout.read lines()
>From me on FC5:
["BASH_EXECUTION _STRING='set | grep IFS'\n", "IFS=' \t\n"]
>From Fredrik Lundh on unknown OS:
["IFS=$' \\t\\n'\n"]
>From Sion Arrowsmith on Debian 1:3.3.5-12:
["IFS=' \t\n"]

I found this problem while developing a CGI shell:
http://code.google.com/p/cgpy-shell/

Although the cgi-shell is working quite well, this issue is a show
stopper for that project. I need a stable/reliable output from Popen().
It looks like each bash or sh version outputs things differently, with
different escaping.

It is not a problem if there is only one or more than one line in the
returned list as i will always get the last one. The problem is the
escaping.

Oct 16 '06 #5
Clodoaldo Pinto Neto wrote:
Output from the shell:

[cpn@s0 teste]$ set | grep IFS
IFS=$' \t\n'

Output from subprocess.Pope n():
>import subprocess as sub
p = sub.Popen('set | grep IFS', shell=True, stdout=sub.PIPE )
p.stdout.readl ines()[1]
"IFS=' \t\n"

Both outputs for comparison:
IFS=$' \t\n'
"IFS=' \t\n"

The subprocess.Pope n() output is missing the $ and the last '

How to get the raw shell output from subprocess.Pope n()?
You are getting the raw shell output, it's just that subprocess uses
the default shell (/bin/sh) and not your personal shell. Try running
/bin/sh and run the set|grep command.

I can't see any obvious way to ask subprocess to use a shell other than
the default.
If you need a particular shell, you could use a shell script, e.g.
myscript.sh:
#!/bin/bash
set|grep IFS

and call that with shell=False
p = sub.Popen('/path/to/myscript.sh', stdout=sub.PIPE )

Oct 16 '06 #6
sj*******@yahoo .com wrote:
I can't see any obvious way to ask subprocess to use a shell other than
the default.
-c ?
>>f = Popen(["/bin/bash", "-c", "set|grep IFS"], stdout=PIPE)
f.stdout.read ()
"IFS=$' \\t\\n'\n"
>>f = Popen(["/bin/sh", "-c", "set|grep IFS"], stdout=PIPE)
f.stdout.read ()
"IFS=' \t\n"

</F>

Oct 16 '06 #7
Fredrik Lundh wrote:
sj*******@yahoo .com wrote:
I can't see any obvious way to ask subprocess to use a shell other than
the default.

-c ?
>>f = Popen(["/bin/bash", "-c", "set|grep IFS"], stdout=PIPE)
>>f.stdout.read ()
"IFS=$' \\t\\n'\n"
>>f = Popen(["/bin/sh", "-c", "set|grep IFS"], stdout=PIPE)
>>f.stdout.read ()
"IFS=' \t\n"
It solves my problem:
>>f = sub.Popen(['/bin/sh', '-c', 'set|grep IFS'], stdout=sub.PIPE )
f.stdout.read ()
"BASH_EXECUTION _STRING='set|gr ep IFS'\nIFS=' \t\n"
>>f = sub.Popen(['/bin/bash', '-c', 'set|grep IFS'], stdout=sub.PIPE )
f.stdout.read ()
"BASH_EXECUTION _STRING='set|gr ep IFS'\nIFS=$' \\t\\n'\n"

But I still don't understand what is happening. The manual says that
when shell=True the executable argument specifies which shell to use:
>>f = sub.Popen('set| grep IFS', shell=True, executable='/bin/sh', stdout=sub.PIPE )
f.stdout.read ()
"BASH_EXECUTION _STRING='set|gr ep IFS'\nIFS=' \t\n"
>>f = sub.Popen('set| grep IFS', shell=True, executable='/bin/bash', stdout=sub.PIPE )
f.stdout.read ()
"BASH_EXECUTION _STRING='set|gr ep IFS'\nIFS=' \t\n"

To make my confusion bigger, in Fedora sh is just a link to bash:
$ ll /bin/*sh
-rwxr-xr-x 1 root root 720888 Feb 11 2006 /bin/bash
lrwxrwxrwx 1 root root 4 Aug 28 22:53 /bin/csh -tcsh
-rwxr-xr-x 1 root root 1175496 Aug 17 13:19 /bin/ksh
lrwxrwxrwx 1 root root 4 Feb 24 2006 /bin/sh -bash
-rwxr-xr-x 1 root root 349312 Aug 17 15:20 /bin/tcsh
-rwxr-xr-x 1 root root 514668 Feb 12 2006 /bin/zsh

Oct 17 '06 #8
Clodoaldo Pinto Neto wrote:
But I still don't understand what is happening. The manual says that
when shell=True the executable argument specifies which shell to use:
no, it says that when shell=True, it runs the command "through" the
default shell. that is, it hands it over to the shell for execution,
pretty much as if you'd typed it in yourself.
To make my confusion bigger, in Fedora sh is just a link to bash:
that only means that the programs share the same binary, not that they
necessarily have exactly the same behaviour:

$ man bash

...

INVOCATION

...

If bash is invoked with the name sh, it tries to mimic the
startup behavior of historical versions of sh as closely as
possible, while conforming to the POSIX standard as well.

...

</F>

Oct 17 '06 #9
Fredrik Lundh wrote:
Clodoaldo Pinto Neto wrote:
But I still don't understand what is happening. The manual says that
when shell=True the executable argument specifies which shell to use:

no, it says that when shell=True, it runs the command "through" the
default shell. that is, it hands it over to the shell for execution,
pretty much as if you'd typed it in yourself.
"If shell=True, the executable argument specifies which shell to use."

Taken from 6.8.1 v2.4:

"The executable argument specifies the program to execute. It is very
seldom needed: Usually, the program to execute is defined by the args
argument. If shell=True, the executable argument specifies which shell
to use. On Unix, the default shell is /bin/sh. On Windows, the default
shell is specified by the COMSPEC environment variable."

Oct 17 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
9462
by: jas | last post by:
I would like to redirect the output from os.system to a variable, but am having trouble. I tried using os.popen(..).read() ...but that doesn't give me exactly what i want. ...this is windows by the way. For example: tmp = os.popen("hostname").read() ....works as expected.
3
5506
by: Darren Dale | last post by:
I'm a developer on the matplotlib project, and I am having trouble with the subprocess module on windows (Python 2.4.2 on winXP). No trouble to report with linux. I need to use _subprocess instead of pywin32, but my trouble exists with either option: import subprocess process = subprocess.Popen(, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) stat = process.wait() print process.stdout.read()
6
17817
by: Kkaa | last post by:
I'm using the os.system command in a python script on Windows to run a batch file like this: os.system('x.exe') The third-party program x.exe outputs some text to the console that I want to prevent from being displayed. Is there a way to prevent the output of x.exe from python?
3
2011
by: johnny | last post by:
How do I pipe the output, generated from os.system(some_command), to the logging file?
14
7437
by: nathan.shair | last post by:
Hello, I searched on Google and in this Google Group, but did not find any solution to my problem. I'm looking for a way to output stdout/stderr (from a subprocess or spawn) to screen and to at least two different files. eg. stdout/stderr -screen
4
13537
by: Tobiah | last post by:
I am not sure how to capture the output of a command using subprocess without creating a temp file. I was trying this: import StringIO import subprocess file = StringIO.StringIO() subprocess.call("ls", stdout = file)
2
10132
by: rparimi | last post by:
I am trying to redirect stderr of a process to a temporary file and then read back the contents of the file, all in the same python script. As a simple exercise, I launched /bin/ls but this doesn't work: #!/usr/bin/python import subprocess as proc import tempfile name = tempfile.NamedTemporaryFile(mode='w+b') print 'name is '+ name.name
25
2792
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the object itself is a subprocess, not a "popen". It seems that it would be more accurate to just name the class Subprocess, can anyone explain why this is not the case? Thank you.
5
4133
by: thedsadude | last post by:
Hello, I'm launching a script as follows: <code> p = subprocess.Popen() p.wait() </code> If p.py writes to sys.stdout, then it is shown on the console.
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.