Armed with this knowledge, you can then use these other file handling calls from the os module [Note that this section is in two parts; be sure to see the next page for more]:
- close(fd): Closes the file descriptor
- dup(fd): Essentially creates a pointer or symbolic link to the file descriptor. The resulting duplicate consequently shares state, file pointer, and file locks. The lowest available descriptor number is used for the resulting duplicate.
- dup2(oldfd, newfd): Duplicates the old descriptor with the new. If the new descriptor already points to another file descriptor, that descriptor is closed first - thus freeing up newfd for use.
- fdopen(fd, mode, bufsize):: Opens a file object to a given file descriptor in a set mode. How much of the file is viewable can be controlled with the bufsize argument. Both mode and bufsize are optional and function similarly to the more common open() function.
- fpathconf(fd, name):Returns configurable pathname variables for fd. name is the name of the variable to be retrieved. Which variables are available depend on the operating system. They usually follow the system header information. POSIX-compliant systems have a bevy of variables which I plan to detail elsewhere on this site (look for the link in due course). However, for most C-based Python installations, you can look at what is available in the header files <limits.h> or <unistd.h> of the C libraries used by Python. Jython users can look similarly at the Java libraries.
- ftruncate(fd, length): Truncate the file fd to a maximum length of length bytes. (available only on Unix derivatives)
- fsync(fd): Forces unwritten data on fd to be written to disk. If fd is buffered (e.g., a Python file object) you are best to flush the data before making this call.