[Tutor] did I lose the value of record?

Eric L. Howard elh at outreachnetworks.com
Tue Mar 2 12:17:57 EST 2004


At a certain time, now past [Mar.02.2004-06:04:44PM +0100], sigurd at 12move.de spake thusly:
> On  2 Mar 2004, Eric L. Howard <- elh at outreachnetworks.com wrote:
> 
> > 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
> > /home/elh
> 
> > 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]
> 
> What is the value of owner here?

it's supposed to be elh...did I ever mention that I hate '\n'?  

> [...]
> 
> > 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'
> 
> That means it can't find the name in your passwd file.  Look at the
> value of owner and look in your passwd file.

Yah...I looked closer this time. The username 'elh' exists in the passwd
file...but the username 'elh\n' doesn't.

Prepending string.strip() to string.split cleans up the values and provides
the expected output.

       ~elh

-- 
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