[Tutor] Parsing /etc/passwd

Gerhardus Geldenhuis gerhardus.geldenhuis at gmail.com
Wed Oct 12 15:41:37 CEST 2011


Hi
I wrote the following code:
  f = open('/etc/passwd', 'r')
  users = f.read()
  userelements = re.findall(r'(\w+):(\w+):(\w+):(\w+):(\w+):(\w+):(\w+)',
users)
  print userelements

  for user in userelements:
    (username, encrypwd, uid, gid, gecos, homedir, usershell) = user  #
unpack the tuple into 7 vars
    print username


but I get no results so my parsing must be wrong but I am not sure why.

Incidentally while googling I found the module
http://docs.python.org/library/pwd.html which I will eventually use but I am
first curious to fix and understand the problem before I throw away this
code.

Regards

-- 
Gerhardus Geldenhuis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111012/ecd5c4fb/attachment.html>


More information about the Tutor mailing list