Question

I am very confused why the code below doesn't work under Python 2.7.6 in OS X.

Code should basically iterate through one list and add items to another list (I want to add conditions later)..

import os
home_dir = os.listdir("/Users/")
users_list = []
for user in home_dir:
    users_list.append(user)

I get the error message below when running it:

Traceback (most recent call last):   File "myfile.py", line x, in
<module>
    users_list.append[suser] TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

/edit: weirdly enough when I do the same thing outside of the file in the python interpreter it seems to work ok?

Was it helpful?

Solution

You should really show the real code which produces the error.

Taken from the Traceback:

users_list.append[suser] - this is wrong

users_list.append(suser) - this is correct

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top