[Tutor] small program, but I'm very confused

Scott Widney SWidney@ci.las-vegas.nv.us
Tue Nov 5 16:35:04 2002


> #creates the file
> f=open('/tmp/listofusers', 'w')

Sidebar: If you are only writing to the file in this process, and you intend
to close it at the end of the process, then the following three lines are
unnecessary

> f.close()
> #opens the file for writing
> f=open('/tmp/listofusers', 'r+')

Correct me if I am wrong here...:

> # searches the password file, hopefully should write the second regexp to
>   thefile
> 
> for y in loginsList:
>
re.findall(('^loginsList[y]:x:[0-9]{4}:[0-9]{2,3}:)([A-Za-z]\s[A-Za-z])
>                (.*?)(/n)','/etc/passwd')

...but it looks like this is discarding the results of re.findall (not
assigning it to anything)...

>     f.write('\2\n')

...and this is trying to write the hexadecimal value x02 and a newline to
the file. Using \2 only applies within the regular expression. If you assign
the results of re.findall() to a list variable, the second element should
correspond to the second group in you regex.

### untested
for y in loginsList:
    groups = re.findall(('^loginsList[y]:x:[0-9]{4}:[0-9]{2,3}:)
                        ([A-Za-z]\s[A-Za-z])(.*?)(/n)','/etc/passwd')
    f.write("%s\n" % (groups[2],))
### or something like that


Scott