【问题标题】:Python 2.7 - crashing on subprocess.pyPython 2.7 - 在 subprocess.py 上崩溃
【发布时间】:2013-12-22 02:22:03
【问题描述】:

Python 2.7 - 在 subprocess.py 上崩溃 - Windows 错误:[错误 2] 系统找不到文件

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

我已经搜索了其余的问题并尝试了所有路径设置和环境变量,一切似乎都很好。

提前感谢您的帮助。

【问题讨论】:

  • 你调用了什么命令?或者是什么python代码导致了这个错误?

标签: python-2.7


【解决方案1】:

通过子进程运行命令时使用列表。 列表应按以下方式创建: 假设你想运行命令:

ls -l

那么命令应该是

cmd = ["ls" "-l" ]
subprocess.Popen(cmd)

【讨论】:

    【解决方案2】:

    “WindowsError: [错误2]系统找不到指定的文件”

    当我想调用一个未安装的程序时,我遇到了同样的错误(我使用的是 ubuntu 而不是 windows)。

    http://docs.python.org/2/library/subprocess.html#exceptions

    尝试在shell中手动执行命令,得到真正的错误

    或者使用这个好方法:

    from subprocess import CalledProcessError, check_output
    
    try:
        output = check_output(["ls", "non existent"])
    except CalledProcessError as e:
        print(e.returncode)
    

    注意:在我的系统 (ubuntu) 中,我得到:

    ls: 无法访问不存在: 没有这样的文件或目录 2

    在windows中没有“ls”命令,你会得到异常意思。

    来自: Check a command's return code when subprocess raises a CalledProcessError exception

    检查这个: Using subprocess to run Python script on Windows

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多