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…
File search
File search is essential to any system administration task. In this section we will build a Python file search script which will search for files based on the given pattern(s) and will also return the file permissions. Like the earlier program, this script will also make use of the commands module to issue the UNIX command ‘find’. We will also make use of a Python module called ‘stat’ to fetch the permissions information.
Code for: filesearch.py
@description: file pattern search script import stat, sys, os, string, commands pattern = raw_input(“Enter the file pattern to search for:\n”) command = “find “ + pattern output = commands.getoutput(command) findResults = string.split(output, “\n”) print “Found Files:” print output print “================================” for file in findResults: mode=stat.S_IMODE(os.lstat(file)[stat.ST_MODE]) print “\nPermissions for file “, file, “:” for level in “USR”, “GRP”, “OTH”: for perm in “R”, “W”, “X”: if mode & getattr(stat,”S_I”+perm+level): print level, “ has “, perm, “ permission” else: print level, “ does NOT have “, perm, “ permission” @code ends @output kunaldeo$ python filesearch.py Enter the file pattern to search for: *.py Found Files: filesearch.py py.py
Permissions for filesearch.py
USR has R permission USR has W permission USR does NOT have X permission GRP has R permission GRP does NOT have W permission GRP does NOT have X permission OTH has R permission OTH does NOT have W permission OTH does NOT have X permission
Permissions for py.py
USR has R permission USR has W permission USR does NOT have X permission GRP has R permission GRP does NOT have W permission GRP does NOT have X permission OTH has R permission OTH does NOT have W permission OTH does NOT have X permission
So now you have an idea of how to use Python in system administration tasks. The following is a list of Python modules that may be of interest to system administrators…
commands: Utility functions for running external commands.
stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat() – in other words, it helps process the permissions.
sys: Access system-specific parameters and functions.
os: Functions to access operations on pathnames.
string: Functions for common string operations.
tty: Utility functions that perform common terminal control operations.
syslog: An interface to the UNIX syslog library routines.















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.