0

Running a python script named automator.py from the command line using powershell on windows 7.

python .\automator.py

automator.py file looks like this....

import os

ipAddressFile = os.path.join("DCM_Info", "ip_address")
ipAddresses = getIpAddresses()

for ip in ipAddresses:
    print str(ip)
    cmd = "python run.py " + ip  + " get_transrator_settings"
    os.system(cmd)


def getIpAddresses():
    f = open(ipAddressFile, 'r')
    return f.readlines()

Why am I getting an error that the name of the method is undefined?

NameError: name 'getIpAddresses' is not defined

I'm used to C#/Java where you have a main method that starts the program and classes that have constructors. Do I need to have a constructor or a class? Is that necessary?

Jonathan Kittell
  • 7,163
  • 15
  • 50
  • 93
  • 1
    in C# or Java the flow of execution is `compile methods -> start main method > .... > finish` so once the `main` has started all the definitions have already run, however in python it is just `start file > ... > finish` so the function is only defined at (and then after) the `def` statement. – Tadhg McDonald-Jensen Apr 25 '16 at 21:53

1 Answers1

3

You need to move the function definition to be before the first time it is used. It can be easy to forget about this, since languages like JavaScript allow functions to be declared after they're called.

mbomb007
  • 3,788
  • 3
  • 39
  • 68