Home  >  Article  >  Backend Development  >  What does fd mean in python?

What does fd mean in python?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-21 13:38:4631182browse

The file descriptor (fd) is the identification number used to uniquely record the currently opened file in the system. fd is an integer.

In addition to the file object, Python also provides operations on fd. The operation on fd is more low-level. The fd and file objects in Python are different concepts. As mentioned when introducing the file object, calling f.fileno() can obtain the fd of a file object, or you can encapsulate a file object on an existing fd: f = os.fdopen(fd).

What does fd mean in python?

#Some fds are allocated in advance when a process is created:

0——Stdin of the process

1 - stdout of the process

2 - stderr of the process

The os module in Python provides the following methods for fd:

1. Close fd

os.close(fd)

2. Copy fd

os.dup(fd)

Return a new fd1, this fd1 is copied Parameter fd.

3. Copy fd

os.dup2(fd, fd2)

Copy fd to fd2. If fd2 is already open, close it first.

4. From fd to file object

os.fdopen(fd, mode='r', bufsize=-1)

Returns a Python file object encapsulating fd. The parameters mode and bufsize correspond to the built-in open() function The parameters have the same meaning.

5. Obtain various attributes of the file from fd

os.fstat(fd)

Return an instance x of stat_result type, and os.stat(path) The returned type is the same, except that the relevant attributes of the corresponding file are obtained through an fd.

6. Change the current position of the file corresponding to fd

os.lseek(fd, pos, how)  

The effect is the same as f.seek(pos, how), the parameter how specifies the reference point, there are three types in total, They are: os.SEEK_SET == 0 (start of file), os.SEEK_CUR == 1 (current position) and os.SEEK_END == 2 (end of file).  

Nothing similar to f.tell() In this way, you can directly use fd to obtain the current position of the file. In fact, you can implement one yourself using os.lseek().

os.lseek(fd, 0, os.SEEK_CUR)

can return the position of the file corresponding to the current fd, and will not modify the original files are affected.

7. Open the file and get fd

os.open(file, flags, mode=0777

The above is the detailed content of What does fd mean in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn