[Tutor] small program, but I'm very confused
Melissa K. Surprenant
msurprenant@siumed.edu
Tue Nov 5 14:23:02 2002
This is a multi-part message in MIME format.
--------------0800B47563D259D80D225317
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
I have to write a small program for class, and I'm having a terrible
time with it. I wrote the program, and it seems to me like it should
work, but it doesn't. I'm sure there are more errors than this, but
right now I get an error when I try to write to the file. It says I
have invalid syntax, with a carat point towards the file name for the
following line: f.write('\2\n')
I would appreciate any help anyone would care to offer. Thank you!
Here is the code:
#!/usr/local/bin/python
import commands
import re
#runs the who command and prints the users
users = [commands.getoutput('who')]
print users
#runs the findall command over users, assigns each one to loginsList
loginsList = re.findall('[a-z]{5}[0-9]{2,3}[a-z]{1}', 'users')
#creates the file
f=open('/tmp/listofusers', 'w')
f.close()
#opens the file for writing
f=open('/tmp/listofusers', 'r+')
#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')
f.write('\2\n')
f.close()
print ("Who's on?")
sorteduserfile = [commands.getoutput('sort /tmp/listofusers')]
print sorteduserfile
--------------0800B47563D259D80D225317
Content-Type: text/x-vcard; charset=us-ascii;
name="msurprenant.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Melissa K. Surprenant
Content-Disposition: attachment;
filename="msurprenant.vcf"
begin:vcard
n:Surprenant;Melissa
tel;pager:217-467-3807
tel;fax:217-545-0192
tel;work:217-545-2132
x-mozilla-html:FALSE
org:SIU School of Medicine;Education & Curriculum
adr:;;801 N. Rutledge;Springfield;IL;62794-9622;United States
version:2.1
email;internet:msurprenant@siumed.edu
fn:Melissa K. Surprenant
end:vcard
--------------0800B47563D259D80D225317--