The most Common POSIX System Calls in Python

The posix module is works on the UNIX environment. It provides the Operating system functionality.

We should not import this module directly. We can use the os module. The os module is acts as a superset of the posix module on UNIX. On non-Unix system the posix is not available, but the os is available with some less functionality.

To use the posix module, we should import it using.

import posix

There are different methods and constants in the POSIX module.

Constant posix.environ

The environ is a dictionary object. It holds keys and values. The keys and values are of bytes type for UNIX. For example, environ[b’HOME’] will show the home directory of the system.

If we modify some entries in this dictionary, it will not affect, the argument of other methods like execv(), popen() etc. To change environment, we need to pass the environ to execve() method. Otherwise add variable assignments and export statement in the command string for system() or popen() method.

Method, mode)

The POSIX open() method can open a file from local disks. It returns a file descriptor. When one file descriptor is closed, the open() method uses the same descriptor again for a new file.

Method, size)

The read() method is used to read a file content using file descriptor. It also takes the maximum limit to read. We can specify the directory, but if it is none, the method will select the current directory.

Example Code

import posix
fruit_fd ="fruits", 0)
print(fruit_fd) #The File Descriptor
val =, 512)
      print("The Home Directory: " + str(posix.environ[b'HOME']))


$ python3
The Home Directory: b'/home/unix_user

Updated on: 25-Jun-2020


Kickstart Your Career

Get certified by completing the course

Get Started