2

I have a python script (gcin.py) which gives me the status of a remote, 0 = down 1 = up.

/usr/local/bin/gcin.py -l 2010
0

I'm trying to run this script inside another python script (GetCut.py) using subprocess but can't get it to recognize the sys.argv[1] variable.

import subprocess,sys
lId=int(sys.argv[1])
p = subprocess.Popen("python /usr/local/bin/gcin.py","-l", lId, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()

if output == 1:
    print "In"
else:
    print "Out"

python GetCut.py 2010

Gives me this error:

Traceback (most recent call last):
  File "GetCut.py", line 4, in <module>
    p = subprocess.Popen("python /usr/local/bin/gcin.py","-l", lId, stdout=subprocess.PIPE, shell=True)
  File "/usr/lib64/python2.7/subprocess.py", line 660, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

I'd appreciate any help.

teamg
  • 703
  • 2
  • 9
  • 15
  • Try being explicit (`bufsize=lId`). – Dan Jan 26 '18 at 20:12
  • a higher-level question: Why call a python file from another python file? You can just make classes/functions that are imported from one to the other. – JacobIRR Jan 26 '18 at 20:25
  • @JacobIRR It allows you to do two things at the same time. In this specific case it may or may not be necessary (The necessity may not be clear if this is a minimal example), but not needing to unnecessarily wait for one class method or function to finish can be a huge help with many problems. I do this all the time when doing automation. – Evan Jan 26 '18 at 21:02

1 Answers1

0

You need to pass the arguments to the subprocess with a list. You also probably don't want to use shell=True.

subprocess.Popen([sys.executable, "/usr/local/bin/gcin.py", "-l", str(lId)], stdout=subprocess.PIPE)

If you only want the output of the program, you can use check_output:

subprocess.check_output([sys.executable, "/usr/local/bin/gcin.py", "-l", str(lId)])

Beefster
  • 723
  • 7
  • 19