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.

4
  • Do you absolutely need to do this from the command line? importing would probably be much easier for this use case Commented Aug 2, 2018 at 7: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 Commented Aug 2, 2018 at 8:00
  • serialize/de-serialize? or just use modules... if you have json you can pass more list data in there Commented Aug 2, 2018 at 8:01
  • Possible duplicate of What is the best way to call a Python script from another Python script? Commented Aug 2, 2018 at 8:04

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.