0

I want to pass the two variables to another python file, i dont want to start it as a sub process.
I want the two processes to run apart as file1 has alot of more calculations to do and cannot wait for file2's operations to complete.

FILE1.PY

include os
name="one"
status="on"
os.system('F:\PythonSub\file2.py' name status)

FILE2.PY

include sys
name=sys.argv[0]
send=sys.argv[1]
print(send, name)

The above code returns

send=sys.argv[1]
IndexError: list index out of range 

What am i doing wrong?

Elo97234c
  • 163
  • 1
  • 4
  • 15
  • Possible duplicate of [How can I run an external command asynchronously from Python?](https://stackoverflow.com/questions/636561/how-can-i-run-an-external-command-asynchronously-from-python) – hnefatl Jul 30 '17 at 11:32

2 Answers2

3

Use the subprocess module (examples on that link). It supports starting processes asynchronously (which os.system doesn't), and passing arguments as parameters. Looks like you'll need something like:

subprocess.call(["F:\PythonSub\file2.py", name, status])

You may want to redirect the stdin/stdout streams if you want to get output from the process, and the shell option may be useful.

Edit: This is wrong, as subprocess.call invokes synchronously, not asynchronously. You should use the approach described in the linked duplicate instead.

hnefatl
  • 5,860
  • 2
  • 27
  • 49
  • Instead of redirecting the `stdout` stream to get the output, you should use `subprocess.check_output` instead of `subprocess.call` – Niema Moshiri Jul 29 '17 at 23:46
1

Try this

import os
name="one"
status="on"
os.system('F:\PythonSub\file2.py %s %s' % (name status))
gipsy
  • 3,859
  • 1
  • 13
  • 21