Using Python for System Programming
In this article, we are going to learn about system programming in Python. We can use Python scripting to automate dull and repetitive monotonous tasks. This can help us to speed up the entire developmental process considerably. The three most important Python modules for system programming are as follows:
- sys module
- os module
- platform module
We will now take a look at a set of small code snippets which help us perform some very useful tasks.
Cleaning up temporary files from Python projects
Often, running some programs can generate quite a lot of temporary files which can quickly clutter our source directories. This can cause a lot of confusion for us. Moreover, a cluttered source could also cause our software to misbehave. We take the example of PyScripter, which is often used by developers to generate standalone executables for particular applications. PyScripter generates a lot of intermediate files, which needs to be cleaned before any subsequent runs of PyScripter. This is because the intermediate files can interfere with subsequent PyInstaller run and may cause it to fail. The following code snippet cleans up the root directory of the project source.
import os,platform print("Deleting autogenerated files from " + os.getcwd() + " ...") os.system('rm -rf *.db __pycache__/ build/ dist/') #os.system('tree') print(platform.system())
Cleaning up cache and temporary system files
In Debian based Linux systems, as we update our system regularly, the package manager caches some of the update packages for faster subsequent downloads. This can quickly build up to take a considerable amount of disk space. We can clean the cache using various shell commands, but having to remember so many commands is difficult and typing them out separately is time-consuming. We can use Python to automate this task for us. This is as shown below:
import os,platform print('Your System: ' + platform.system()) print('Your System Info: ' + str(os.uname())) os.system('sudo du -h /var/cache/apt/archives') print('Cleaning cached package archives ....') os.system('sudo apt-get clean') print('Successfully cleaned cache.')
PLEASE NOTE: YOU NEED TO RUN THE ABOVE PYTHON SCRIPT AS A SUPERUSER.
Configuring Environment for a project
Sometimes, when we divide a large software project into separate Python files and organize them into separate directories, we face problems importing a Python file from a different directory. This is because we need to add that directory to our path environment variable. The following code snippet is just an example of how we can use the various functions available to us from the os module.
import sys,os,platform #print(sys.path) print('Adding ' + os.getcwd() + ' to PATH Environment Variable . . . .') sys.path.append(os.getcwd()) #print(sys.path) print(os.listdir()) print(os.sep) l = os.path.split(os.getcwd()) print(l) print(os.path.join(l,l))
One might ask the necessity of the above python applications when we can just execute the above commands in a bash shell (Linux) or Command Prompt (Windows) to get the job done. There are two reasons for doing this:
- We can automate the sequential execution of a set of commands.
- Implements a certain level of platform independence.
In this article, we saw some ways in which we can use the various modules like os, platform and sys to automate some of the system administration tasks.
To know more about the OS module, in particular, see the following article: OS Module in Python