Just Me
Just Me

Reputation: 125

Array on python function

Please help. It's really children's question but I'm at a loss. Why I can't to return array?

There is my script :

groups=[]

host_groups_list = '/usr/local/host_groups.list'

def read_file(file_path):
        open_file = open(host_groups_list, "r+")
        list=[]
        for i in open_file:
                list.append(str(i.replace("\n", "")))
        print list
        return list

goups = read_file(host_groups_list)
print groups

Output :

['hostgroup1', 'hostgroup2']
[]

Upvotes: 0

Views: 52

Answers (1)

Mike Müller
Mike Müller

Reputation: 85442

Spelling is important:

goups = read_file(host_groups_list)
print groups

Note the missing r in goups.

You don't need the groups=[] in the beginning. Delete it and Python will give a name error for your print statement.

Better don't use list as a name for your variables because it shadows a built-in.

Upvotes: 3

Related Questions