Insights from Our Experts
How Python makes system programming easier
Python has come a long way from being around in the shadow of C++ and Java to become one of the most popular programming languages. Its flexibility with data science applications will further elevate its status and usage. The simple programming syntax and code readability makes Python very easy to use general-purpose web applications as well as business enterprise applications.
Also, System programming with Python is a whole lot easier with the help of the os module. It serves as an abstract layer between the python program and the operating system. The main advantage of using python for system programming is that most commands are independent of the OS. All the functions we discuss here require us to import the os module first. You can do it by using the 'import os' function.
The aim of the article is to introduce you with some of the basic functions of the OS module.
Let us go into the details one by one.
We are using Ubuntu 14.04 in the examples.
Environ returns a copy of strings representing the environment. It is returned in the form of a key-value pair.
eg. here os.environ['HOME'] gives me my home folder location.
It returns the current working directory. Here in the picture it's showing that my working directory is '/bin'
It is used to change the current directory.
It's used to make a directory
It's used to delete a directory.
It's used to rename a file or directory. The syntax is as follows,
It is used to change the owner and group id of a path to the provided owner and group id. To leave a parameter unchanged you can use -1. The syntax is as follows,
In the example I change the ownership of the file to user with id 1001 and leave the group id unchanged.
The system function allows Python programs to run and execute system commands.
It allows us to kill a process. The syntax is as follows:
In the below example, notice that the process with id 7833 has been killed.
Now that you have a basic knowledge feel free to dive into the official documentation right here.
Read also: Python optimization techniques