473,405 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Subprocess confusion: how file-like must stdin be?

Question:
import subprocess, StringIO

input = StringIO.StringIO("abcdefgh\nabc\n")
# I don't know of a compact, evocative, and
# cross-platform way to exhibit this behavior.
# For now, depend on cat(1).
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE,
stdin = response)

Why this is a question:
A. it tosses an AttributeError.
B. I *expected* it to do the equivalent of
cat << HERE
abcdefgh
abc
HERE

In <URL: http://docs.python.org/dev/lib/node530.html >, I read "Valid
values are ... an existing file object ..." Even though StringIO is
a "file-like object", it lacks a fileno. Is there a way to get what
I'm after?
Aug 17 '06 #1
5 8605
Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
On Thu, 17 Aug 2006 17:16:25 +0000, cl****@lairds.us (Cameron Laird)
declaimed the following in comp.lang.python:
Question:
import subprocess, StringIO

input = StringIO.StringIO("abcdefgh\nabc\n")

Here you override the builtin function "input()"
# I don't know of a compact, evocative, and
# cross-platform way to exhibit this behavior.
# For now, depend on cat(1).
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE,
stdin = response)

Here you specify the non-existant "response"
Assume the OP meant to write this
>>import subprocess, StringIO
inp = StringIO.StringIO("abcdefgh\nabc\n")
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE, stdin = inp)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/subprocess.py", line 534, in __init__
(p2cread, p2cwrite,
File "/usr/lib/python2.4/subprocess.py", line 830, in _get_handles
p2cread = stdin.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
>>>


--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Aug 18 '06 #2
In article <sl*****************@irishsea.home.craig-wood.com>,
Nick Craig-Wood <ni**@craig-wood.comwrote:
>Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
> On Thu, 17 Aug 2006 17:16:25 +0000, cl****@lairds.us (Cameron Laird)
declaimed the following in comp.lang.python:
Question:
import subprocess, StringIO

input = StringIO.StringIO("abcdefgh\nabc\n")

Here you override the builtin function "input()"
# I don't know of a compact, evocative, and
# cross-platform way to exhibit this behavior.
# For now, depend on cat(1).
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE,
stdin = response)

Here you specify the non-existant "response"

Assume the OP meant to write this
>>>import subprocess, StringIO
inp = StringIO.StringIO("abcdefgh\nabc\n")
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE, stdin = inp)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/subprocess.py", line 534, in __init__
(p2cread, p2cwrite,
File "/usr/lib/python2.4/subprocess.py", line 830, in _get_handles
p2cread = stdin.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
>>>>
Aug 18 '06 #3
Cameron Laird a écrit :
In article <sl*****************@irishsea.home.craig-wood.com>,
Nick Craig-Wood <ni**@craig-wood.comwrote:
>Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
>> On Thu, 17 Aug 2006 17:16:25 +0000, cl****@lairds.us (Cameron Laird)
declaimed the following in comp.lang.python:

Question:
import subprocess, StringIO

input = StringIO.StringIO("abcdefgh\nabc\n")
Here you override the builtin function "input()"
# I don't know of a compact, evocative, and
# cross-platform way to exhibit this behavior.
# For now, depend on cat(1).
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE,
stdin = response)
Here you specify the non-existant "response"
Assume the OP meant to write this
>>>>import subprocess, StringIO
inp = StringIO.StringIO("abcdefgh\nabc\n")
p = subprocess.Popen(["cat"], stdout = subprocess.PIPE, stdin = inp)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/subprocess.py", line 534, in __init__
(p2cread, p2cwrite,
File "/usr/lib/python2.4/subprocess.py", line 830, in _get_handles
p2cread = stdin.fileno()
AttributeError: StringIO instance has no attribute 'fileno'
.
.
.
Yes; my apologies for the confusion I introduced by "editing
for publication", and doing it badly.

Your interactive session does indeed exhibit the behavior that
puzzles me. My expectation was that StringIO and the std*
parameters to Popen() were made for each other; certainly there
are many cases where stdout and stderr can be redirected *to* a
StringIO. Is it simply the case that stdin demands a more
file-like object? While that disappoints me, I certainly can
program around it. My question, then: does stdin effectively
require something really in the filesystem, or perhaps the
stdout of a previous subprocess? Is there no built-in way to
feed it an in-memory construct?
As this is a pipe at OS level, there may be no other way than using
os-level tools (ie. real files with fileno), maybe creating an anonymous
pipe, writing to it (relying on pipe buffering by the OS to avoid the
creation of a writer thread), and giving this pipe (which should have a
fileno) to the subprocess.Popen stdin parameter.
Such a construction (pipe/writer thread) would be welcome as standard
subprocess tool.
A+

Laurent.

Aug 18 '06 #4
Cameron Laird wrote:
Your interactive session does indeed exhibit the behavior that
puzzles me. My expectation was that StringIO and the std*
parameters to Popen() were made for each other; certainly there
are many cases where stdout and stderr can be redirected *to* a
StringIO. Is it simply the case that stdin demands a more
file-like object? While that disappoints me, I certainly can
program around it. My question, then: does stdin effectively
require something really in the filesystem, or perhaps the
stdout of a previous subprocess? Is there no built-in way to
feed it an in-memory construct?
set the appropriate stream to subprocess.PIPE, and write to it.

p = subprocess.Popen(..., stdin=subprocess.PIPE)
p.stdin.write("hello")
p.stdin.close() # signal end of file

</F>

Aug 18 '06 #5
In article <ma***************************************@python. org>,
Fredrik Lundh <fr*****@pythonware.comwrote:
>Cameron Laird wrote:
>Your interactive session does indeed exhibit the behavior that
puzzles me. My expectation was that StringIO and the std*
parameters to Popen() were made for each other; certainly there
are many cases where stdout and stderr can be redirected *to* a
StringIO. Is it simply the case that stdin demands a more
file-like object? While that disappoints me, I certainly can
program around it. My question, then: does stdin effectively
require something really in the filesystem, or perhaps the
stdout of a previous subprocess? Is there no built-in way to
feed it an in-memory construct?

set the appropriate stream to subprocess.PIPE, and write to it.

p = subprocess.Popen(..., stdin=subprocess.PIPE)
p.stdin.write("hello")
p.stdin.close() # signal end of file
Aug 18 '06 #6

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

Similar topics

6
by: Uri Nix | last post by:
Hi all, I've been trying to use (Python 2.4 on WinXP) the subprocess module to execute a shell command (nmake in this case), and pass its output to a higher level. Using the following...
2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
3
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...
3
by: madpython | last post by:
playing with subprocess.Popen on Windows I stumbled into the following problem: Python 2.4.3 (#69, Mar 29 2006, 17:35:34) IDLE 1.1.3 >>> import subprocess >>>...
3
by: Ivan Vinogradov | last post by:
Dear All, I would greatly appreciate a nudge in the right direction concerning the use of cwd argument in the call function from subprocess module. The setup is as follows: driver.py <-...
3
by: Tim Arnold | last post by:
Hi, Just discovered that my subprocess call with the preexec_fn wasn't doing what I thought. If 'machine' value is different than the current machine name, I want to remsh the command to that...
5
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that...
1
by: bahoo | last post by:
Hi, I am using Windows + Python 2.5. This line of code fails (see error message at the end), last_line = subprocess.Popen(, stdout=subprocess.PIPE).communicate() but using "os.system"...
12
by: Eric_Dexter | last post by:
I am trying to modify a programming example and I am coming up with two problems... first is that I can't seem to pass along the arguments to the external command (I have been able to do that with...
2
by: dudeja.rajat | last post by:
On Mon, Sep 8, 2008 at 11:50 AM, <dudeja.rajat@gmail.comwrote: Ok, I re-phrase my question: there is a batch file that executes a exe file. The batch just works if run from command prompt and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.