简体   繁体   中英

get error while I call .exe file from my python script

I use wxpython for GUI and bash for script. I have to run a .exe file from a Python script using subprocess.

Purpose: Must pass parameter from GUI to the .exe file, and don't have permission to check it.

Part of my code where I am getting the problem is:

import subprocess
def OnBound(self,event):
lan1 = self.sc1.Getvalue() ##interger value
arg = ('home/proj/lic.exe')
subprocess.call([lan1, arg], shell = True)

Whenever I run my script I get the following error:

Traceback (most recent call last)
File "/usr/lib/python 2.7/subprocess.py", line 493, in call return popen(*popnargs, **kwargs).wait()
File "/usr/lib/python 2.7/subprocess.py", line 679, in __init__errread,errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1239, in _execute_child raise child_exception
Type error: execv() arg 2 must contain only strings

What may I have done wrong here? Any help / suggestions would be helpful as I am new to python.

All items in the first parameter of subprocess.call must be strings:

rc = subprocess.call(['/home/proj/lic.exe', str(lan1)])

Also you shouldn't call functions that can block for a long time from a GUI event handler; it can freeze your GUI for a long time. You could call subprocess.Popen to return immediately instead and schedule an idle callback to poll the subprocess state periodically.

I'm not sure what you are trying to accomplish, however I can reproduce your error using

import subprocess
subprocess.call([123, 'ls'], shell = True)

or

import subprocess
subprocess.call(['ls', ('-l',)], shell = True)

In both cases I pass garbage as one of the list values (integer in first case, and tuple in the second case).

Comment in your code tells that lan1 is an integer, so you are trying to run something like

123 home/proj/lic.exe

which does not make sense.

Also it is simpler to use subprocess like this:

subprocess.call("ls -l | grep test", shell=True) 

i think the error is quite clear: "execv() arg 2 must contain only strings" while you are passing an integer as the first argument. converting integers to strings can be done with str()

apart from that: subprocess.call takes an array of strings, where the 1st string is the program to be called, and the rest are the arguments to be passed to this program. so if you want to call home/proj/lic.exe with an argument 123 (or whatever the value of lan1 ), you ought to switch the order.

subprocess.call([arg, str(lan1)], shell = True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM