【问题标题】:i am getting an error using subprocess command我在使用 subprocess 命令时遇到错误
【发布时间】:2020-04-28 11:39:51
【问题描述】:

这是python代码

import subprocess

cmd = "HelloWorld.c"
# Example
# cmd = HelloWorld.c
print("Hey this is Python Script Running\n")
subprocess.call(["gcc", cmd])  # For Compiling
subprocess.call("./a.out", shell = True)

# end thats all

这是 C 代码

#include<stdio.h>

 int main()
 {
     printf("Hello World\n");
     printf("This C program Running\n");
     return 0;
 }

这是错误代码。

嘿,这是正在运行的 Python 脚本

Traceback (most recent call last):
  File "C:/Users/Yogi/PycharmProjects/Last_Subprocess_Try/Subprocess/Test.py", line 7, in <module>
    subprocess.call(["gcc", cmd])  # For Compiling
FileNotFoundError: [WinError 2] The system cannot find the file specified
Process finished with exit code 1

【问题讨论】:

  • 您的搜索路径中有gcc 吗?如果我调用一个不存在的可执行文件,我只能得到那个错误。
  • 我正在运行这个 i Pycharm Community edition 2019,你能详细说明如何添加 GCC 路径。

标签: python python-3.x subprocess


【解决方案1】:

你应该给出确切的 HelloWorld.c 路径

cmd = "/home/user/../HelloWorld.c"

【讨论】:

  • 如果 executable 不能使用给定的参数运行,Python 不会抛出错误。 gcc 只会说“gcc: error: hello: No such file or directory”
  • 对不起,你错了。这是子进程错误来自操作系统而不是 python。所以文件路径错误
  • cmd ="/Users/Yogi/Desktop/Pycharm Old Projects/Last_Subprocess_Try/Subprocess/HelloWorld.c" 我给出了拉取路径,但它仍然无法工作@Beyhan Gul
  • .. 但我试过了!如果找不到gcc,我会得到确切的FileNotFoundError OP 报告。如果 gcc 找不到文件,Python 根本不在乎。
  • 是的。我很抱歉。 @TheBatman,您应该正确提供 gcc 路径。首先确保它安装正确
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 2021-09-18
  • 2020-10-15
  • 1970-01-01
  • 2014-10-13
  • 1970-01-01
  • 2013-08-01
相关资源
最近更新 更多