[Tutor] First real program
Mr 804
mr804@users.757.org
Fri, 2 Feb 2001 12:07:08 -0500 (EST)
Hello,
I'm trying to write a small python script to do some checks
on a unix password file. I've run into a stubling block. I think I don't
understand list 100%. I've included my code with commends on the problem
part.
******************************************
import sys
import string
import re
### open files read it in 1 line at a time
print "Loading the password file."
try:
f = open('/etc/passwd','r')
s = f.readlines()
f.close()
print "loaded."
print ""
except IOError:
print "can't open the password file. Quiting."
sys.exit()
### How many elements do we got?
t = len(s)
print "There are %d lines." % t
## sort it
s.sort()
# Clean the file up. Don't need no white space before or after
# also convert everything to lower case.
list = []
name = []
for i in s:
i = string.lower(string.rstrip(string.strip(i)))
list.append(i)
name.append(string.splitfields(i,":"))
^--
This part should make everything lower case, strip white space.
name[] should be a list of NAMES, but I don't think I'm doing that
correctly? I want the user name and no othe fields after the first
: . ? if I did print name[0] it should print the first name in the
password file. but it's just printing the while line.