Python – Linux : file descriptors from os. pipe() does not appear in /dev/fd

Linux : file descriptors from os. pipe() does not appear in /dev/fd… here is a solution to the problem.

Linux : file descriptors from os. pipe() does not appear in /dev/fd

In the past, I saw all the virtual files for open file descriptors appear in /dev/fd. However, currently I’m using Amazon Linux, and when I run os.pipe() in my Python program, I don’t see the new file descriptor appear.

For example:

MASTER:hadoop@imrdasem2d14$ ls -l /dev/fd/
total 0
lrwx------ 1 hadoop hadoop 64 Jul 23 15:39 0 -> /dev/pts/0
lrwx------ 1 hadoop hadoop 64 Jul 23 15:39 1 -> /dev/pts/0
lrwx------ 1 hadoop hadoop 64 Jul 23 15:39 2 -> /dev/pts/0
lr-x------ 1 hadoop hadoop 64 Jul 23 15:39 3 -> /proc/30933/fd
MASTER:hadoop@imrdasem2d14$ %
python
>>> import os
>>> a,b = os.pipe()
>>> c,d = os.pipe()
>>>
[2]+  Stopped                 python
MASTER:hadoop@imrdasem2d14$ !ls
ls -l /dev/fd/
total 0
lrwx------ 1 hadoop hadoop 64 Jul 23 15:39 0 -> /dev/pts/0
lrwx------ 1 hadoop hadoop 64 Jul 23 15:39 1 -> /dev/pts/0
lrwx------ 1 hadoop hadoop 64 Jul 23 15:39 2 -> /dev/pts/0
lr-x------ 1 hadoop hadoop 64 Jul 23 15:39 3 -> /proc/31001/fd
MASTER:hadoop@imrdasem2d14$ 

But it’s clear that the pipe is working :

a,b,c,d
(3, 4, 5, 6)
>>> os.write(b,b"foo")
3
>>> os.read(a,3)
b'foo'

So why are my file descriptors for 3, 4, 5, and 6 not in /dev/fd?

Solution

The contents of /dev/fd are process-specific. You are looking at the file descriptor of the ls process, not the file descriptor of the Python process.

Have your Python process check what:

os.listdir('/dev/fd')

For example:

>>> import os
>>> os.listdir('/dev/fd/')
['0', '1', '2', '3']
>>> a,b = os.pipe()
>>> os.listdir('/dev/fd/')
['0', '1', '2', '3', '4', '5']
>>>

Related Problems and Solutions