System programming with Python is made a lot easy with the help of the os module, it serves as an abstract layer between the python program and the operating system, thus the main advantage of using python for system programming is that most commands are independent of the OS i.e, it is platform independent. All functions we discuss here require us to import the os module first,you can do it by using 'import os'.
Let us go in detail into some functions of the os module. 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 example below notice that the process with id 7833 has been killed.
That's it! Now that you have a basic knowledge feel free to dive into the official documentation right here.