File object wrapper for a String?

D

David Erickson

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
 
C

Christian Heimes

David said:
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
 
D

Diez B. Roggisch

David said:
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
 
C

Christian Heimes

Diez said:
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
 
D

David Erickson

You don't need a file like object:

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

Christian

Perfect. Thanks.

-David
 
D

David Erickson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top