[CentralOH] Popen file descriptor difference between Python 2.6 and 2.7+ on Linux

Eric Floehr eric at intellovations.com
Fri Feb 16 22:34:35 EST 2018


I have a really odd problem. I have some code that requires a Linux file
descriptor (/dev/fd/N) where N is some number. The following example code
demonstrates effectively what is being done.

In order to run the code create a file called "/tmp/test" with some text in
it.

The code works in Python 2.6, but in Python 2.7 or later, I get:

cat: /dev/fd/4: No such file or directory

Can anyone figure out why? And what needs to change for the following code
to work on 2.7 and later. Assume that I cannot change needing file
descriptors (/dev/fd/4 for example):

Here is the code:

-------------------------------------------------
from subprocess import Popen, PIPE

data = Popen(
    ["cat"],
    stdin=open('/tmp/test', 'rb'),
    stdout=PIPE,
    universal_newlines=True).stdout

fd_name = '/dev/fd/%d' % data.fileno()

fddata = Popen(
    ["cat",
    fd_name],
    stdout=PIPE,
    universal_newlines=True,).stdout

print(fddata.read())
-------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20180216/f0d222e3/attachment.html>


More information about the CentralOH mailing list