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.
There are different methods and constants in the POSIX module.
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.
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.
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.
import posix fruit_fd = posix.open("fruits", 0) print(fruit_fd) #The File Descriptor val = posix.read(fruit_fd, 512) print(val) print("The Home Directory: " + str(posix.environ[b'HOME']))
$ python3 posix_example.py 3 b'Mango\nOrange\nBanana\nApple\nGuava\nGrape\nRaspberry\nBlueberry\nPineapple\nWatermelon\n' The Home Directory: b'/home/unix_user