Python development masterclass
It may seem like Python is just another scripting language, but know that when you are using Linux, the chances are there is some Python code working backstage helping you. Among other things, this article looks into the versatile nature of Python and its many uses…
Checking for root user
Some commands only work in root mode. So it will be very helpful if we can check whether the current user is root or not. The following script performs this job. Root is associated with User ID (UID) 0. Hence we are checking for that value to determine who is a root user.
Code for: checkroot.py
@description: Check for root user import os if os.getuid() == 0: print(“You are a root user !”) else: print (“You are a regular guy .”) @ @output kunal@Dell-Work:~/python$ python checkroot.py You are a regular guy . kunal@Dell-Work:~/python$ sudo python checkroot.py You are a root user ! @
Process information
Occasionally a system administrator might need to look into the details of a process that is causing problems. In order to do this we will make use of the commands module which contains wrapper functions for os.popen() which take a system command as a string and return any output generated by the command and, optionally, the exit status. In this case we are running the ps command and then formatting the output of ps according to our needs. This will only work on UNIX systems as we are making use of ps.
Code for:psinfo.py
@Description: Process Information import commands, os, string myps = raw_input(“Enter the Process Name: “) result = commands.getoutput(“ps -ef|grep “ + myps) psinfo = string.split(result) print “\n\ Terminal:\t\t”, psinfo[5], “\n\ Owner:\t\t\t”, psinfo[0], “\n\ Process ID:\t\t”, psinfo[1], “\n\ Parent process ID:\t”, psinfo[2], “\n\ Time started:\t\t”, psinfo[4] @ @output: kunal@Dell-Work:~/python$ python psinfo.py Enter the Process Name: konsole Terminal: pts/0 Owner: kunal Process ID: 1977 Parent process ID: 1 Time started: 14:30 @
















Please state which old python version and other programs you are using. It is obvious from your print statements that it is not 3.1.2 which is recommended for ALL new development.
@Frank Python 3.1.2 is not recommended for development as of now. If you had read the article completely you would have known that Python currently maintains two mutually incompatible branch of the 2.X and 3.X. Until 3.X is finalized 2.X will remain in the usage. FYI: 2.X is still used in 95% of the installed applications.
Trackbacks
What's your opinion?