Suppose we want parse the /etc/passwd
file,
root:x:0:0:root:/root:/bin/bash
sshd:x:71:65:SSH daemon:/var/lib/sshd:/bin/false
ilan:x:500:100:Ilan Schnell:/home/ilan:/bin/tcsh
...
and create a dictionary of the uid
mapping to the username.
#!/usr/bin/python
uidmap={}
for line in file('/etc/passwd'):
lst = line.split(':')
uidmap[lst[2]] = lst[0]
while True:
print uidmap[raw_input('Please enter uid: ')]
Program in action:
$ ./uidmap.py
Please enter uid: 500
ilan
Please enter uid: 71
sshd