marengz
marengz

Reputation: 117

os.exec. passing arguments to new file function

I have 2 separate .py files and i want to open a second one from the first and pass arguments to it using os.execlp. Please can you assist on how to pass the arguments.

1st file:

def parent():

  a = input("value one:  ")
  b = input("value two:  ")
  os.execlp('python', 'python', 'product.py') 

2nd file:

def product(a,b):

   print("product is", a*b)

Upvotes: 0

Views: 462

Answers (1)

Himanshu
Himanshu

Reputation: 2454

As these are separate processes 2nd file should accept command line arguments like this :-

def product(a, b):
    print ("product is", a*b)

if __name__ == "__main__":
    product( int(sys.argv[1]), int(sys.argv[2]) )

Verify that this indeed multiplies two numbers :-

python product.py 2 3

product is 6

And then invoke it from the first file:-

a = input("value one:  ")
b = input("value two:  ")
os.execlp('python', 'python', 'product.py', a, b) 

Upvotes: 1

Related Questions