0
votes

I'm trying to print the uid which is greater than max_uid, but I have the following error. Can someone let me know what I'm missing here ?

max_uid = 0
for line in open("/etc/passwd"):
split = line.split(":")
if int(split[2]) > max_uid:
    max_uid = int(split[2])

print(max_uid)

Thanks in advance

1
Some line has fewer than 2 colons :, so it doesn't get split into 3 parts - Patrick Haugh

1 Answers

1
votes

Without providing the file I can't tell you for sure, but it seems that the list split has a length less than 3, which which cause

if int(split[2]) > max_uid:
    max_uid = int(split[2])

to throw an IndexError. This can be tested by running this

for line in open("/etc/passwd"):
split = line.split(":")
if len(split) < 3:
    print("WILL ERROR")

It checks if any of the split values have a length less than 3, causing you to be unable to index split[2] (remember python lists start at index 0)