Home > Articles > Open Source > Python

Python Libraries

  • Print
  • + Share This

UNIX Specific

This group of modules exposes interfaces to features that are specific to the UNIX environment.


The posix module provides access to the most common POSIX system calls. Do not import this module directly; instead, I suggest that you import the os module.

>>> uid = posix.getuid()  # returns the user id


The pwd module provides access to the UNIX passwd (password database) file routines.


Returns the password of a given user.

basic syntax: password = getpwnam(username)[1]

>>> import pwd, getpass
>>> pw = pwd.getpwnam(getpass.getuser())[1]


The grp module provides access to the UNIX group database.


The crypt module offers an interface to the UNIX crypt routine. This module has a hash function based on a modified DES algorithm that is used to check UNIX passwords.

To encrypt:

newpwd = crypt.crypt(passwordstring, salt)

salt consists of a two-random character seed used to initialize the algorithm.

To verify:

If newpwd == crypt.crypt(passwordstring, newpwd[:2])
import getpass
import pwd
import crypt

uname = getpass.getuser()  # get username from environment
pw = getpass.getpass()   # get entered password

realpw = pwd.getpwnam(uname)[1] # get real password
entrpw = crypt.crypt(pw, realpw[:2]) # returns an encrypted password
if realpw == entrpw:    # compare passwords
  print "Password Accepted"
  print "Get lost."


The dlmodule module exposes an interface to call C functions in shared objects that handle dynamically linked libraries. Note that this module is not needed for dynamic loading of Python modules. The documentation says that it is a highly experimental and dangerous device for calling arbitrary C functions in arbitrary shared libraries.


The dbm module is a database interface that implements a simple UNIX (n)dbm library access method. dbm objects behave like dictionaries in which keys and values must contain string objects. This module allows strings, which might encode any python objects, to be archived in indexed files.

See Chapter 8 for details.


The gdbm module is similar to the dbm module. However, their files are incompatible. This module provides a reinterpretation of the GNU dbm library.

See Chapter 8 for details.


The termios module provides an interface to the POSIX calls for managing the behavior of the POSIX tty.


The TERMIOS module stores constants required while using the termios module.


The tty module implements terminal controlling functions for switching the tty into cbreak and raw modes.


The pty module offers utilities to handle the pseudo-terminal concept.


The fcntl module performs file and I/O control on UNIX file descriptors. This module implements The fcntl() and ioctl() system calls, which can be used for file locking.


The pipes module offers an interface to UNIX shell pipelines. By abstracting the pipeline concept, it enables you to create and use your own pipelines.


The posixfile module provides file-like objects with support for locking. It seems that this module will become obsolete soon.


The resource module offers mechanisms for measuring and controlling system resources used by a program.


The nis module is a thin wrapper around Sun's NIS library.


The syslog module implements an interface to the UNIX syslog library routines. This module allows you to trace the activity of your programs in a way similar to many daemons running on a typical GNU/Linux system.

import syslog
syslog.syslog('This script was activated' )
print "I am a lumberjack, and I am OK!"
syslog.syslog('Shutting down script' )

Use the command tail -f /var/log/messages to read what your script is writing to the log.


The popen2 module allows you to create processes by running external commands and to connect their accessible streams (stdin, stdout, and stderr) using pipes.

import os,popen2
str1 = os.popen('ls','r').read()
print str1
out1,in1 = popen2.popen2('cat')
str2 = out1.read()
print str2


Note that as of release 2.0, functions popen2, popen3, popen4 are supported on the Windows Platform.


The commands module provides functions that execute external commands under UNIX by implementing wrapping functions for the os.popen() function. Those functions get a system command as a string argument and return any output generated by that command.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.