1

I am trying to develop a code between two python program where I pass a command line argument for the second program to execute from the first program.

3 Answers3

0

One approach would be to call a function from the second program instead of using command-line arguments. You can do this by importing the said function.

Another approach would be to use command-line arguments as you said, with the command System or the Subprocess module.

afterm3
  • 69
  • 1
  • 5
0

main.py

import os
os.system('python3 test.py 1 2 3')

Other program's main file

test.py

import sys

print('Number of arguments:', len(sys.argv), 'arguments.')
print('Argument List:', str(sys.argv))

Output

Number of arguments: 4 arguments.
Argument List: ['test.py', '1', '2', '3']

Run main.py which runs test.py file with arguments.

Vashdev Heerani
  • 660
  • 7
  • 21
0

Try this in your first_program.py :

import os
os.system('python3.6 /path/to/second_program.py arg1')

These look related :

Run a Python script from another Python script, passing in arguments

How can I make one python file run another?

What is the best way to call a script from another script?

Zee
  • 21
  • 1
  • 2