简体   繁体   English

Popen在命令行上找不到可用的批处理文件

[英]Popen can't find batch file available on command line

I'm trying to execute a program which is available to my command prompt but isn't in Python. 我正在尝试执行程序,该程序可用于命令提示符,但不适用于Python。

Command prompt: 命令提示符:

C:\Users\Documents\libexe\tfc\bin\Debug>asc-dir
asc-dir.: directory not linked to an ASC directory //Expected output

Test Script: 测试脚本:

proc = subprocess.Popen('asc-dir', stdout=subprocess.PIPE)
(result, err) = proc.communicate()

print(result)   

Error: 错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.1\visualstudio_py_util.py", line 1
06, in exec_file
    exec_code(code, file, global_variables)
  File "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensio
ns\Microsoft\Python Tools for Visual Studio\2.1\visualstudio_py_util.py", line 8
2, in exec_code
    exec(code_obj, global_variables)
  File "C:\Users\mryan.\git\web\PBNBApi\pbnb_cli\test.py", lin
e 9, in <module>
    proc = subprocess.Popen('asc-dir', stdout=subprocess.PIPE)
  File "C:\Python27\lib\subprocess.py", line 709, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 957, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
Press any key to continue . . .

You seem to be running through some Visual Studio extension. 您似乎正在运行某些Visual Studio扩展。 Maybe it fiddles with the PATH env var passed to python? 也许它把传递给python的PATH env var摆弄了? As such, you may need to specify the absolute path. 因此,您可能需要指定绝对路径。

import os
print os.environ['PATH']

Looks like I needed to set shell=True 看起来我需要设置shell=True

proc = subprocess.Popen('asc-dir', stdout=subprocess.PIPE, shell=True)
(result, err) = proc.communicate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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