0

I have a sample program like

File.py

def test1():
    print("test1")
def test2():
    print("test2")
def test3():
    print("test3")
def main():
    sys.argv[1]
    sys.argv[2]
    sys.argv[3]
 #   test1()
 #   test2()
 #   test3()

main()

I need to execute a command like "python3 File.py test1() test2() test3()"

some scenarios I need to execute only one or two methods only "python3 File.py test1() test2()"

chinna
  • 55
  • 1
  • 8

2 Answers2

1

I am not sure if this is what you were looking for, but if you open the CMD in the directory your file.py is saved in, you can use:

python -i file.py

This will open the file in the interactive shell. You can then access the functions with a normal function call, like test1().

10 Rep
  • 2,217
  • 7
  • 19
  • 33
Athos
  • 109
  • 5
1

I would fill out the main function as follows:

arguments = list(sys.argv)
arguments.pop(0)
while arguments:
    function_name = arguments.pop(0)
    locals()[function_name]()

Then, in command line you type:

$ python3 file.py test1 test3 test2
Kate Melnykova
  • 1,863
  • 1
  • 5
  • 17