我正在尝试在Windows中使用python执行以下命令并WindowsError: [Error 2] The system cannot find the file specified
遇到错误,当前我没有script.exe
在PC上手动运行它会引发错误'script.exe' is not recognized as an internal or external command,operable program or batch file.
,我希望通过python运行会引发相同的错误,如何解决此错误?真的很感谢任何输入
代码:-
cmd = "script.exe"
print "Executing " + cmd
fetchPipe = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, error) = fetchPipe.communicate()
def find_abs_path(executable_fname):
if os.path.exists(executable_fname): # in case it is in our cwd
return os.path.abspath(os.path.join(".",executable_fname))
for dirname in os.environ["PATH"].split(";"): # split dependant on your os
if executable_fname in os.listdir(dirname):
return os.path.join(dirname,executable_fname)
应该搜索您的路径并找到可执行文件的绝对路径
这是推荐的方法...
...但是您可以传入一个环境
subprocess.Popen(cmd_args,env=os.environ,...)
这应该向运行的子外壳提供相同的PATH变量...这样就可以找到可执行文件...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句