I am trying to write a numpy array data to a pipe opened using subprocess.Popen in Python 3.4. Here is the code
import numpy
import subprocess
myArray = numpy.arange(10000).reshape([100,100])
fullCmd = "xpaset DS9Test array [xdim=100,bitpix=64,arch=littleendian,ydim=100]"
mp = subprocess.Popen(
fullCmd,
shell = True,
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
bufsize = 0
)
myArray.tofile(mp.stdin)
Unfortunately I am getting the following error:
File "/Users/avigan/Work/HC-HR/FTS/test.py", line 25, in <module>
myArray.tofile(mp.stdin)
OSError: first argument must be a string or open file
However, if I do:
print(mp.stdin)
<_io.FileIO name=71 mode='wb'>
I interpret this as a sign that the file descriptor is indeed open.
Anyone sees what wrong here?
Not exactly the solution what you ask for, but I had the same problem of wanting to redirect the numpy array to a PIPE so I could redirect it into feedgnuplot to build a histogram.
Instead, I ended up using a temporary file as follows:
import os
import tempfile
command = " | feedgnuplot --terminal 'dumb 160,40' --histogram 0 --with boxes --unset grid --exit"
with tempfile.NamedTemporaryFile('w+t', suffix=".txt") as f:
np.savetxt(f, myArray, fmt='%.4f')
f.seek(0)
os.system("sudo more " + f.name + command)
While your usage requirement is probably different, you can still probably read back the temporary file for your own application.
HTH
According to the docs, this should be equivalent to tofile
for writing binary data:
mp.stdin.write(myArray.tobytes())
See if it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.