
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
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
- Related Articles
- The fcntl and ioctl System Calls in Python
- What are system calls in Operating System?
- What are the different system calls in the operating system?
- Python - Most common Combination in Matrix
- How are system calls connected to the operating system?
- System Calls in Unix and Windows
- Timer in C++ using system calls
- Different types of system calls
- What is the purpose of System Calls?
- What are the Process Management System Calls?
- Find most common element in a 2D list in Python
- What are the types of system calls used in file management?
- The Most Common Email Marketing Mistakes
- Name the most common fire extinguisher.
- Differentiate between Application Programming Interfaces (APIs) and system calls.
