Basic IO and File IO

For output, the repr(x) and str(x) functions can be used to convert values to strings. str(x) is intended to produce the human-readable version, while repr(x) generates representations that can be read by the interpretter.

For formatting output strings, the % symbol can be used much like in sprintf in C, e.g.:

print '%2d %f %s' % (3, 2.1, 'foo')
You can also call various formatting methods directly on the string objects, e.g.
print mystr.ljust(width)   # left justify
print mystr.rjust(width)   # right justify
print mystr.center(width)  # center
print mystr.zfill(width)   # pad on left with zeros
For file IO, the open(filename, mode) method returns a file object. For example:
fptr = open('filename', 'r')  # r for read, w for write, a for append, etc
Once you've opened a file object, there are a variety of available methods:

To read from standard input, import sys and use sys.stdin as your filename, e.g.
somevar = sys.stdin.readline()

There are two possible exceptions: EOFError (for end of file) and IOError (for other read/write errors).

The combination of I/O and string manipulation methods Python provides makes it very handy for quick file parsing. The example below gets the current user's id, then opens up the system password file to look up other information about them.

#! /usr/bin/env python

# we'll use the posix module for the getuid routine
#       and the string module for the splitfields
import posix
import string

# look up the current user's id, and store it as text
uid = `posix.getuid()`

# open the password file
passwdfile = open('/etc/passwd')

# read lines from the password file until you find one
#      with a matching userid
for nextline in passwd.readlines():
        # the password file fields are delimited with colons,
        #     so use splitfields to break the line into an array
	nextrecord = string.splitfields(nextline, ':')
        # we also know that in the password file
        #    the userid is the third field, 
        #    and the username is the first field
        if nextrecord[2] == uid:
        	username = nextrecord[0]
        	print 'hello', username
        	found = 1
		break

# once we've processed the file, check to make sure we
#      successfully identified the user
if found == 0:
	print 'I could not find you in the password file'

OS file and directory handling

The os and os.path modules provide a variety of routines for accessing and manipulating files and directories.

Some of the commonly used routines include:

A couple of useful cross-platform variables exist for identifying the different symbols used to denote current and parent directories, and for seperating elements of a path:

For example, the following identifies and displays all the files/subdirectories in/below the current directory:

#! /usr/bin/env python

import os, os.path

def explore(args, curdir, contents):
    for nextfile in contents:
        if nextfile == os.curdir:
            continue
        elif nextfile == os.pardir:
            continue
        elif os.path.isfile(nextfile):
            print "File", nextfile, "\n"
        elif os.path.isdir(nextfile):
            print "Directory", nextfile, "\n"
        else:
            continue

os.path.walk(os.getcwd(), explore, '')