May-08-2020, 05:18 PM
line 8 is the root of your problem
g[3] is a string and you iterate over string. so u is each char in that string. then you try to split single char at ',' (line 9)
what i suggest:
g[3] is a string and you iterate over string. so u is each char in that string. then you try to split single char at ',' (line 9)
what i suggest:
def get_group_users(): f = open('group.example', 'r') #f is file users = [] #obvious for l in f: g = l.strip().split(':') #g is group if (g[0] == "users"): users.extend(g[3].split(',')) return users print(get_group_users())
Output:['kintaro', 'john', 'autossh', 'test']
Now, what I would do, to make better codedef get_group_users(file_name): users = [] with open(file_name, 'r') as f: for line in f: if line.startswith('users'): *_, group_users = line.strip().split(':') users.extend(group_users.split(',')) return users print(get_group_users('group.example'))
Output:['kintaro', 'john', 'autossh', 'test']
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs