Python Forum
Python: Call function with variabele? Ending in error.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python: Call function with variabele? Ending in error.
#1
Hi all,
I am a beginner in Python and I am trying to create a script that is login to all kind of different devices (Storage, server, switches) and collect information out of these devices.
What I am doing is I open an csv file, read the lines and with every line I call a (ssh)funtion to login to that device. Because the devices are diverend I have multiple fuctions for every device model.
I try to create the funtion name into a variable because it could be different and I need to figure out which funtion to call with this variable, but I get an error and I am really stuck here.

Thanks for helping and I'll hope my question make sense.

def ssh_login_xxx(host_ip):
    do the login stuf

def ssh_login_yyy(host_ip):
    do the login stuf

def ssh_login_zzz(host_ip):
    do the login stuf

with open (input_file, 'r') as my_file:
    csv_reader = csv.DictReader(my_file)
    for line in csv_reader:
        dev_model = line['dev_model']
        host_ip = line['host_ip']

		sub = dev_model
        if sub == dev_model:
            function_name = ("ssh_login_"+sub) 
            print(function_name)
            function_name(host_ip)
Error:
Traceback (most recent call last): File "c:/scripting/python/Training/login.py", line 86, in <module> function_name(host_ip) TypeError: 'str' object is not callable
#lines in csv file:
dev_model,host_ip
xxx,192.0.0.10
yyy,192.0.0.11
zzz,192.0.0.12
xxx,192.0.0.13
Reply


Messages In This Thread
Python: Call function with variabele? Ending in error. - by efclem - Apr-22-2020, 07:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 752 May-02-2023, 08:40 AM
Last Post: Gribouillis
  While loop not ending (Best of 10 dice game) K3nidi 3 1,456 Jul-09-2022, 09:53 AM
Last Post: K3nidi
  UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 error from Mysql call AkaAndrew123 1 3,384 Apr-28-2021, 08:16 AM
Last Post: AkaAndrew123
  how to call an object in another function in Maya bstout 0 2,042 Apr-05-2021, 07:12 PM
Last Post: bstout
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 3,416 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Struggling for the past hour to define function and call it back godlyredwall 2 2,156 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  list call problem in generator function using iteration and recursive calls postta 1 1,862 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  function call at defined system time? Holon 5 3,154 Oct-06-2020, 03:58 PM
Last Post: snippsat
  How to call/read function for all elements in my list in python johnny_sav1992 1 2,038 Jul-27-2020, 04:19 PM
Last Post: buran
  Ending the Program Twoshawns 2 2,051 May-19-2020, 02:24 AM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020