# pydoc > fcntl

---
type: CommandReference
command: fcntl
mode: pydoc
section: ""
source: pydoc3
---

## Quick Reference

- `fcntl.fcntl(fd, cmd, arg=0)` — Perform file control operation
- `fcntl.flock(fd, operation)` — Apply or remove an advisory lock
- `fcntl.ioctl(fd, request, arg=0, mutate_flag=True)` — I/O control on descriptor
- `fcntl.lockf(fd, operation, len=0, start=0, whence=0)` — Lock/unlock file region

## Name

fcntl — This module performs file control and I/O control on file descriptors. It is an interface to the `fcntl()` and `ioctl()` Unix routines. File descriptors can be obtained with the `fileno()` method of a file or socket object.

## Synopsis

python
import fcntl
## Functions

### `fcntl.fcntl(fd, cmd, arg=0)`

Perform the operation `cmd` on file descriptor `fd`. The values for `cmd` are OS‑dependent constants available in the `fcntl` module (same names as in C header files). `arg` is optional, defaults to 0; may be an int or a string.

- If `arg` is a string, the return value is a string of that length containing the result buffer from the OS. The string length must not exceed 1024 bytes.
- If `arg` is an int or omitted, the return value is an int corresponding to the return value of the C `fcntl()` call.

### `fcntl.flock(fd, operation)`

Perform the lock operation `operation` on file descriptor `fd`. See the Unix manual page for [flock(2)](https://man7.org/linux/man-pages/man2/flock.2.html). On some systems this function is emulated using `fcntl()`.

### `fcntl.ioctl(fd, request, arg=0, mutate_flag=True)`

Perform the I/O operation `request` on file descriptor `fd`. The values for `request` are OS‑dependent constants in the `fcntl` or `termios` modules. `arg` is optional, defaults to 0; may be an int, a string, or a mutable buffer (e.g., an array).

- If `arg` is a mutable buffer and `mutate_flag` is True, the buffer is passed to the OS and changes are reflected after the call. The return value is the integer returned by the `ioctl()` system call.
- If `arg` is a mutable buffer and `mutate_flag` is False, behavior is as if a string had been passed.
- If `arg` is an immutable buffer (string), a copy is passed and the return value is a string of the same length containing the OS result. The buffer length must not exceed 1024 bytes.
- If `arg` is an int or omitted, the return value is an int.

### `fcntl.lockf(fd, operation, len=0, start=0, whence=0)`

A wrapper around the `fcntl()` locking calls. `fd` is the file descriptor, `operation` is one of:

- `LOCK_UN` — unlock
- `LOCK_SH` — acquire a shared lock
- `LOCK_EX` — acquire an exclusive lock

When operation is `LOCK_SH` or `LOCK_EX`, it can be bitwise ORed with `LOCK_NB` to avoid blocking. If `LOCK_NB` is used and the lock cannot be acquired, an `OSError` is raised with `errno` set to `EACCES` or `EAGAIN`.

`len` is the number of bytes to lock (default to EOF). `start` is the byte offset relative to `whence`. `whence` is as with `fileobj.seek()`:

- `0` — relative to start of file (`SEEK_SET`)
- `1` — relative to current buffer position (`SEEK_CUR`)
- `2` — relative to end of file (`SEEK_END`)

## Data

The module defines the following constants (values are OS‑dependent; typical Linux values shown):

- `DN_ACCESS = 1`, `DN_ATTRIB = 32`, `DN_CREATE = 4`, `DN_DELETE = 8`, `DN_MODIFY = 2`, `DN_MULTISHOT = 2147483648`, `DN_RENAME = 16`
- `FASYNC = 8192`
- `FD_CLOEXEC = 1`
- `F_ADD_SEALS = 1033`, `F_DUPFD = 0`, `F_DUPFD_CLOEXEC = 1030`, `F_EXLCK = 4`, `F_GETFD = 1`, `F_GETFL = 3`, `F_GETLEASE = 1025`, `F_GETLK = 5`, `F_GETLK64 = 5`, `F_GETOWN = 9`, `F_GETPIPE_SZ = 1032`, `F_GETSIG = 11`, `F_GET_SEALS = 1034`, `F_NOTIFY = 1026`, `F_OFD_GETLK = 36`, `F_OFD_SETLK = 37`, `F_OFD_SETLKW = 38`, `F_RDLCK = 0`, `F_SEAL_GROW = 4`, `F_SEAL_SEAL = 1`, `F_SEAL_SHRINK = 2`, `F_SEAL_WRITE = 8`, `F_SETFD = 2`, `F_SETFL = 4`, `F_SETLEASE = 1024`, `F_SETLK = 6`, `F_SETLK64 = 6`, `F_SETLKW = 7`, `F_SETLKW64 = 7`, `F_SETOWN = 8`, `F_SETPIPE_SZ = 1031`, `F_SETSIG = 10`, `F_SHLCK = 8`, `F_UNLCK = 2`, `F_WRLCK = 1`
- `LOCK_EX = 2`, `LOCK_MAND = 32`, `LOCK_NB = 4`, `LOCK_READ = 64`, `LOCK_RW = 192`, `LOCK_SH = 1`, `LOCK_UN = 8`, `LOCK_WRITE = 128`

## See Also

- Python documentation: [fcntl module](https://docs.python.org/3/library/fcntl.html)
- man pages: [fcntl(2)](https://man7.org/linux/man-pages/man2/fcntl.2.html), [ioctl(2)](https://man7.org/linux/man-pages/man2/ioctl.2.html), [flock(2)](https://man7.org/linux/man-pages/man2/flock.2.html)
- Related modules: `os`, `termios`