- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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.
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.
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.
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
- The fcntl and ioctl System Calls in Python
- Python - Most common Combination in Matrix
- What are the different system calls in the operating system?
- What are system calls in Operating System?
- How are system calls connected to the operating system?
- What is the purpose of System Calls?
- What are the Process Management System Calls?
- Different types of system calls
- System Calls in Unix and Windows
- Timer in C++ using system calls
- What are the most common teenage problems?
- What are the most common mental illnesses?
- Find most common element in a 2D list in Python
- Which is the most common lost article in India?
- What are the types of system calls used in file management?