Search This Blog

Wednesday, June 25, 2014

Python for SysAdmins

Preparing for a interview some time ago I made a list of python module interesting for SysAdmins. Today looking for something else I found that half baked note and decided to polish it enough to put it on the blog. It's mostly for myself as quick reference.

Each module is describe by one, two sentences (from Python documentation) and have a link to official online document. At the end there is a list of example function, object.

Python modules for SysAdmin

import sys

This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available.


  • argv,
  • exit(),
  • path,
  • modules,
  • exec().


import os

This module provides a portable way of using operating system dependent functionality.


  • chdir(),
  • getuid(),
  • uname(),
  • listdir(),
  • stat(),
  • rename(),
  • access().


import os.path

This module implements some useful functions on pathnames.


  • isdir(), 
  • isfile(), 
  • exist(), 
  • getmtime(), 
  • abspath(), 
  • join(), 
  • basename(), 
  • dirname().


import time

This module provides various time-related functions.


  • time(), 
  • ctime(), 
  • sleep(), 
  • strftime(), 
  • strptime().


import glob

The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell. No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched.


  • glob(),
  • iglob().


import fnmatch

This module provides support for Unix shell-style wildcards, which are not the same as regular expressions (which are documented in the re module).


  •  fnmatch().


import re

This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings as well as 8-bit strings.


  • compile(), 
  • match(), 
  • search(), 
  • split(), 
  • findall(), 
  • sub(),
  • group().


Match objects always have a boolean value of True. Since match() and search() return None when there is no match, you can test whether there was a match with a simple if statement:

match =, string)
if match:

Post a Comment