473,700 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subprocess confusion: how file-like must stdin be?

Question:
import subprocess, StringIO

input = StringIO.String IO("abcdefgh\na bc\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.Pope n(["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 8648
Dennis Lee Bieber <wl*****@ix.net com.comwrote:
On Thu, 17 Aug 2006 17:16:25 +0000, cl****@lairds.u s (Cameron Laird)
declaimed the following in comp.lang.pytho n:
Question:
import subprocess, StringIO

input = StringIO.String IO("abcdefgh\na bc\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.Pope n(["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.String IO("abcdefgh\na bc\n")
p = subprocess.Pope n(["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.net com.comwrote:
> On Thu, 17 Aug 2006 17:16:25 +0000, cl****@lairds.u s (Cameron Laird)
declaimed the following in comp.lang.pytho n:
Question:
import subprocess, StringIO

input = StringIO.String IO("abcdefgh\na bc\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.Pope n(["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.String IO("abcdefgh\na bc\n")
p = subprocess.Pope n(["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.net com.comwrote:
>> On Thu, 17 Aug 2006 17:16:25 +0000, cl****@lairds.u s (Cameron Laird)
declaimed the following in comp.lang.pytho n:

Question:
import subprocess, StringIO

input = StringIO.String IO("abcdefgh\na bc\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.Pope n(["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.String IO("abcdefgh\na bc\n")
p = subprocess.Pope n(["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.Pope n 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.Pope n(..., stdin=subproces s.PIPE)
p.stdin.write(" hello")
p.stdin.close() # signal end of file

</F>

Aug 18 '06 #5
In article <ma************ *************** ************@py thon.org>,
Fredrik Lundh <fr*****@python ware.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.Pope n(..., stdin=subproces s.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
1734
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 snippet: p = subprocess.Popen(nmake,stderr=subprocess.PIPE,stdout=subprocess.PIPE, \ universal_newlines=True, bufsize=1)
2
4082
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 open a python interpreter and run my subprocess command, all is well. But when I run the same command from inside a Tkinter app, I'm getting errors.
3
5503
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()
3
12747
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 >>> p1=subprocess.Popen("c:\\asd.bat") #works OK >>> p2=subprocess.Popen("c:\\asd.bat",stdout=subprocess.PIPE)
3
3593
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 <- python script core/ <- directory main <- fortran executable in the core directory
3
1659
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 machine, but obviously I misunderstood the preexec_fn arg. Should I just put the remsh in the actual command instead of preexec_fn? thanks, --Tim Arnold
5
14322
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 consistently gets an "invalid operand" IOError when write() is called on the pipe. So I switched to subprocess. It works fine when executed "normally" (e.g. "python progname.py"), but when bundled by py2exe, it always does this:
1
3601
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" works: os.system('D:/release/win.exe 0.5 1000 100 D:/images/img.ppm out.ppm')
12
10984
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 the old module and cmd is the command I wish to try) all the output seems to be returned as one line (at least when I run the program in spe). import subprocess from os import system cmd = """gawk -f altertime.awk -v time_offset=4 -v
2
2742
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 produces output to standard output and the file. Now, I try to call the same batch file from subprocess.Pope() call. The batch file gets called and that internally also calls the exe
0
8726
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
8645
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
9075
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
8973
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6561
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
4404
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...
1
3089
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2028
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.