- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 posix.open(file_name, 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 posix.read(file_descriptor, 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 = 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']))
Output
$ python3 posix_example.py 3 b'Mango\nOrange\nBanana\nApple\nGuava\nGrape\nRaspberry\nBlueberry\nPineapple\nWatermelon\n' The Home Directory: b'/home/unix_user