[Tutor] did I lose the value of record?

Eric L. Howard elh at outreachnetworks.com
Tue Mar 2 11:30:43 EST 2004

I have a file that contains two columns delimited by ":".

I can open the file for reading, re.search through the lines for a
particular record and string.split the record into two parts which are
assigned to two different variables.  It's the third assignment that's
causing the script to fall over on it's face [the pwd.getpwnam]

In the interpreter - this works:

>>> record  = "elhtest:elh"
>>> userhome = pwd.getpwnam(string.split(record, ":")[1])[5]
>>> print userhome

However - this doesn't appear to work:

#!/usr/bin/env python

import re, string, pwd

listingsfile = open("listings","r")
for record in listingsfile.readlines():
    if re.search("elhtest", record):
        listing = string.split(record, ":")[0]
        owner = string.split(record, ":")[1]
        ownerhome = pwd.getpwnam(string.split(record, ":")[1])[5]
        print "The listing we're found is %s" % listing
        print "The owner for this listing is %s" % owner
        print "The owner's home dir is %s" % ownerhome

I get the following:

Traceback (most recent call last):
  File "./sa_setup.py", line 10, in ?
    ownerhome = pwd.getpwnam(string.split(record, ":")[1])[5]
KeyError: 'getpwnam(): name not found'



Eric L. Howard           e l h @ o u t r e a c h n e t w o r k s . c o m
www.OutreachNetworks.com                                    313.297.9900
JabberID: elh at jabber.org                 Advocate of the Theocratic Rule

More information about the Tutor mailing list