0

I have a python file name "a.py" and another python file name "b.py". I wanted to run a.py with command in b.py file and I use this codes below:

import os

def b():
    os.system("python a.py")

but a.py has a list name "list_exam" that I want to pass it to b.py through the command line code. I don't know how to pass it in a.py and how to get with command in b.py file.

thanks a lot.

MMRA
  • 337
  • 1
  • 3
  • 11
  • Do you absolutely need to do this from the command line? importing would probably be much easier for this use case – FlyingTeller Aug 02 '18 at 07:59
  • in fact, my main command is os.system("python b.py -c configs/conf.json") and I want to send different config each time. @FlyingTeller – MMRA Aug 02 '18 at 08:00
  • serialize/de-serialize? or just use modules... if you have json you can pass more list data in there – Jean-François Fabre Aug 02 '18 at 08:01
  • Possible duplicate of [What is the best way to call a Python script from another Python script?](https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-python-script-from-another-python-script) – Cezar Cobuz Aug 02 '18 at 08:04

2 Answers2

2

Please don't use shell commands to invoke other Python scripts, it is neither efficient nor scalable.

if a.py:

def do_somthing(param1, param2):
    ...
    list_exam = make_result()
    return list_exam

def main(config):
    do_something(config['param1'], config['param2'])

you can write b.py like this:

import a

def b():
    list_exam = a.do_something(config['param1'], config['param2'])

don't forget to create a __init__.py file in that dir.

Doron Cohen
  • 1,026
  • 8
  • 13
0

You may want to use sys.argv to get command line parameters. Take a look at this: https://www.pythonforbeginners.com/system/python-sys-argv

But, maybe your question is already answered here: Passing a List to Python From Command Line

Autrion
  • 1
  • 2