-2

One of my python script is giving me several output (for eg. 1067879400 1067880600 1067881200 1067881800 1067882400 1067883000 1067883600). Now I want to supply these numbers or arguments to another python script. So how can I include both scripts in one script so that output from first script is given to another script as command line arguments.

Thanks

-Viral

physics_for_all
  • 1,771
  • 3
  • 16
  • 20
  • 1
    Please show both the scripts ? – ZdaR Apr 28 '15 at 08:58
  • [This answer](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) shows how you can call something from the command line from python. – Robin Apr 28 '15 at 08:59
  • 1
    Depending on your situation, it might be a good idea to keep the scripts separate and only pipe the results between the scripts with the shell. Something like `script1.py | script2.py`. In order for this to work you would have to make sure your scripts are writing (and reading) from STDIN/OUT. – Lix Apr 28 '15 at 09:01
  • You don't need to pipe or anything else like that, either keep it to a single script or import the functions. – Padraic Cunningham Apr 28 '15 at 09:04
  • Actually I cannot combine two scripts because other script contains several ipython modules which will not work with python. So basically I am looking for how to add command line arguments in call function? from subprocess import call call('test2.py' ) here I want to pass arguments – physics_for_all Apr 28 '15 at 09:11
  • How could they not work if you have two python scripts, that makes no sense? – Padraic Cunningham Apr 28 '15 at 09:13

2 Answers2

2

If you really have to do it this way, you can consider something like:

os.system("script2.py arg1 arg2")

In the case of a list or dict, you can consider using pickle when preparing the data to be passed/received.

Reuben L.
  • 2,626
  • 2
  • 24
  • 41
  • How will this pass args from one script to another? – Padraic Cunningham Apr 28 '15 at 09:56
  • Hi Reuben, how can I use `pickle` in this case? My script1.py gives my output like `num=['1067881800','1067881200','1067882400','1067880600','1067883000','1067883600','1067879400']` so how can I pass this argument to script2.py? Right now I am using `os.system(" python script2.py "+str(NUM))` in my `script1.py`. But it is not working. – physics_for_all Apr 28 '15 at 11:51
  • `pickle.dump()` and `pickle.load()`. You can consider using @geckon's pipe solution with this also. – Reuben L. Apr 28 '15 at 12:14
1

Please consider the following options:

1) run the two scripts from your shell using a pipe like this:

first.py | second.py

2) re-write the scripts so you can import the first script in the second, for example like this:

the first script:

# first.py

# the method returning the numbers as a list (formerly putting them to stdout)
def get_numbers():
    # do something to collect the list of the numbers
    return numbers_list

the second script:

# second.py
from .first import get_numbers

# the method using the numbers (formerly getting them from stdin)
def process_numbers():
    numbers = get_numbers()
    # do something to process the numbers

If you really want to call the other script as is, you can do it this way:

#second.py
from subprocess import Popen, PIPE

def process_numbers():
    p = Popen(["first.py", "argument"], stdout=PIPE, stderr=PIPE)
    out, err = p.communicate()
    # the out variable now contains the standard output of the first script
    # do something to process the numbers
geckon
  • 7,063
  • 2
  • 28
  • 55