[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

File object wrapper for a String?

David Erickson

2/14/2008 11:03:00 PM

I am using the Popen class from subprocess and would like to feed a
string in as the stdin parameter, however primarily it only takes a
File object. Does there exist a wrapper for a string to do this? I
know I can just dump the string to a temp file and feed that in but
that is pretty hacky.

Thanks,
David
5 Answers

Christian Heimes

2/14/2008 11:07:00 PM

0

David Erickson wrote:
> I am using the Popen class from subprocess and would like to feed a
> string in as the stdin parameter, however primarily it only takes a
> File object. Does there exist a wrapper for a string to do this? I
> know I can just dump the string to a temp file and feed that in but
> that is pretty hacky.

You don't need a file like object:

popen = subprocess.Popen(..., stdin=subprocess.PIPE)
popen.stdin.write("yourstring")

Christian

Diez B. Roggisch

2/14/2008 11:16:00 PM

0

David Erickson schrieb:
> I am using the Popen class from subprocess and would like to feed a
> string in as the stdin parameter, however primarily it only takes a
> File object. Does there exist a wrapper for a string to do this? I
> know I can just dump the string to a temp file and feed that in but
> that is pretty hacky.

Christian told you how to work with popen. Just for the record thogh:
there are the StringIO and cStringIO-modules.

Diez

Christian Heimes

2/14/2008 11:24:00 PM

0

Diez B. Roggisch wrote:
> Christian told you how to work with popen. Just for the record thogh:
> there are the StringIO and cStringIO-modules.

IIRC subprocess requires a real file with a file descriptor for the
standard streams. An object with a write or read method isn't enough.

Christian

David Erickson

2/14/2008 11:25:00 PM

0

On Feb 14, 3:07 pm, Christian Heimes <li...@cheimes.de> wrote:
> David Erickson wrote:
> > I am using the Popen class from subprocess and would like to feed a
> > string in as the stdin parameter, however primarily it only takes a
> > File object. Does there exist a wrapper for a string to do this? I
> > know I can just dump the string to a temp file and feed that in but
> > that is pretty hacky.
>
> You don't need a file like object:
>
> popen = subprocess.Popen(..., stdin=subprocess.PIPE)
> popen.stdin.write("yourstring")
>
> Christian

Perfect. Thanks.

-David

David Erickson

2/14/2008 11:25:00 PM

0

On Feb 14, 3:16 pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
> David Erickson schrieb:
>
> > I am using the Popen class from subprocess and would like to feed a
> > string in as the stdin parameter, however primarily it only takes a
> > File object. Does there exist a wrapper for a string to do this? I
> > know I can just dump the string to a temp file and feed that in but
> > that is pretty hacky.
>
> Christian told you how to work with popen. Just for the record thogh:
> there are the StringIO and cStringIO-modules.
>
> Diez

Good to know, thanks Diez.

-David