What are file descriptors and directories?

Asked

Viewed 1,172 times

3

I’m studying the módulo os of the standard library of Python and I realize 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 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 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?

1 answer

4


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

On 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 when using system calls.

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

Directory descriptors are the same thing, but they serve to manipulate a file system directory. There are also other descriptors like Pipes or network (sockets).

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

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

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

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

I mean, that’s what it’s for, whenever I want to reference this file that’s 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 finish the answer by recalling 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 your programs. You rarely need to use one of these methods.

  • Thank you again for that enlightening reply. I thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.