What are file and directory descriptors?

2

I am studying módulo os of the Python default library and I realize that in many cases a function is allowed to receive as a parameter 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 A 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   an option that might work on one could 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 specifies 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 questions are:

  • What is descritor de arquivo ?
  • What is a descritor de diretório aberto ?
asked by anonymous 26.10.2018 / 05:04

1 answer

4

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 the file and directory descriptors.

On UNIX-based systems, when you have an open input and output feature, it is associated with a nonnegative integer, which is called descritor ... A file descriptor is therefore an indicator that is used for refer to this file when using system calls.

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

Directory descriptors are the same, however, they are used to manipulate a file system directory. There are also other descriptors such as pipes or sockets.

For example, we can use os.open() to make a low-level call by passing the flag to hexadecimal 0x41, which indicates that we want to create a file in writing 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 whole number 3. All the low-level operations I'm going to do with the file, I need to pass that number 3:

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

I mean, whenever you 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 reminding you that these low-level methods should not be used in normal programs; They are more difficult to operate properly, and can be causes of bugs in their programs. You rarely need to use one of these methods.

    
26.10.2018 / 06:49