<p dir="auto">Piping a numpy array as binary data to another process works in Python2, but in Python3 the following snippet results in "OSError: first argument must be a string or open file". I tested with Python 3.4.0.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import subprocess, numpy
wc = subprocess.Popen([&quot;wc&quot;], stdin=subprocess.PIPE)
numpy.eye(5).tofile(wc.stdin)"><pre class="notranslate"><code class="notranslate">import subprocess, numpy
wc = subprocess.Popen(["wc"], stdin=subprocess.PIPE)
numpy.eye(5).tofile(wc.stdin)
</code></pre></div>
<p dir="auto">I am trying to write a numpy array data to a pipe opened using subprocess.Popen in Python 3.4 and numpy 1.10.4. Here is the code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy
import subprocess

myArray = numpy.arange(10000).reshape([100,100])

# use xpaset to communicate with ds9
fullCmd = &quot;xpaset DS9Test array [xdim=100,bitpix=64,arch=littleendian,ydim=100]&quot;

mp = subprocess.Popen(
    fullCmd, 
    shell = True, 
    stdin = subprocess.PIPE, 
    stdout = subprocess.PIPE, 
    stderr = subprocess.STDOUT,
    bufsize = 0
)

myArray.tofile(mp.stdin)"><pre class="notranslate"><code class="notranslate">import numpy
import subprocess

myArray = numpy.arange(10000).reshape([100,100])

# use xpaset to communicate with ds9
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)
</code></pre></div>
<p dir="auto">Unfortunately I am getting the following error:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  File &quot;/Users/avigan/Work/HC-HR/FTS/test.py&quot;, line 25, in &lt;module&gt;
    myArray.tofile(mp.stdin)

OSError: first argument must be a string or open file"><pre class="notranslate"><code class="notranslate">  File "/Users/avigan/Work/HC-HR/FTS/test.py", line 25, in &lt;module&gt;
    myArray.tofile(mp.stdin)

OSError: first argument must be a string or open file
</code></pre></div>
<p dir="auto">However, if I do:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [64]: print(mp.stdin)
&lt;_io.FileIO name=71 mode='wb'&gt;"><pre class="notranslate"><code class="notranslate">In [64]: print(mp.stdin)
&lt;_io.FileIO name=71 mode='wb'&gt;
</code></pre></div>
<p dir="auto">I interpret this as a sign that the file descriptor is indeed open.</p>
<p dir="auto">The same code was working fine in Python 2.7, so I guess it's linked to Python 3+ and the current version of numpy.</p>