What are file and directory descriptors?

I am studying the módulo os of the standard library of Python and I notice that in many cases a function is allowed to receive as a parameter a descritor de arquivo ou diretório some examples are:

os.fchdir(fd) Change the current working directory to the directory represented by the file descriptor fd. The descriptor must refer to an opened directory, not an open file. As of Python 3.3, this is equivalent to ' os.chdir (fd).

os.supports_fd to Set object indicating which functions in the os module permit specifying their path parameter as an open file descriptor. Different platforms provide different functionality, and year option that might work on one might be unsupported on another. For consistency's sakes, functions that support fd always allow specifying the parameter, but will raise an exception if the functionality is not actually available.

To check whether a particular function permits specifying an open file descriptor for its path parameter, use the in operator on supports_fd. As an example, this expression determines whether os.chdir () accepts open file descriptors when called on your local platform:

os.chdir in os.supports_fd

My doubts are:

  • what is a descritor de arquivo?
  • what is a descritor de diretório aberto?
Author: ThiagoO, 2018-10-26

1 answers

The os module gives access to certain low-level features, rarely used in normal python programming. An example is these functions that deal directly with file and directory descriptors.

In UNIX-based systems, when it has an open input and output feature, it is associated with a non-negative integer, which is called descritor... A file descriptor is therefore an indicator that is used to reference this file to the use system calls.

File descriptors are part of the POSIX programming API, so every unix-like has this feature.

Directory descriptors are the same thing, however, they serve to manipulate a file system directory. There are also other descriptors such as pipes or network (sockets).

For example we can use os.open() to make a low-level call, passing as a parameter the hexadecimal flag 0x41 which indicates that we want to create a file in write mode:

>>> fd = os.open('/tmp/teste.txt', 0x41)
>>> print(fd)
3

The function return is the "file descriptor" for the open file. In this case the integer 3. All low-level operations I'm going to do with the file, I need to pass this number 3:

>>> os.write(fd, b'hello')
5   # < - número de bytes escritos

That is, it serves this purpose, whenever I want to refer to this file that is open, I can pass the number 3:

>>> os.write(3, b' world\n')
7
>>> os.close(3)

Checking the contents of the file:

>>> open('/tmp/teste.txt').read()
'hello world\n'

I end the answer by going back to remember that these low-level methods should not be used in normal programs; they are more difficult to operate properly, and may be causes of bugs in your programs. One rarely has to use one of these methods.

 4
Author: nosklo, 2018-10-26 04:49:41