首页 > 解决方案 > Python Subprocess.check_output() and call() will not properly execute commands (Windows 10)

问题描述

So I'm currently trying to run a python script that will call the command line to compile (MinGW) a .cpp script, and then run the executable. Here is the .py:

import time
import subprocess
from subprocess import Popen, PIPE, check_output

# Start the clock again
tic2 = time.time()

try:
    # This is Python's way of calling the command line. We use it to compile the C++ files.
    subprocess.check_output(["g++ BubbleSort.cpp"],stdin=None,stderr=subprocess.STDOUT,shell=True)
except subprocess.CalledProcessError as e:
    # There were compiler errors in BubbleSort.cpp. Print out the error message and exit the program.
    print("<p>",e.output,"</p>")
    raise SystemExit

p = Popen(['./a.exe'], shell=True, stdout=PIPE, stdin=PIPE)
print(p.stdout.read())

# End clock
toc2 = time.time()
print("C++ Bubble Sort finished in %0.6f seconds" % (toc2 - tic2))

However, every time I run this script through the CLI (I'm using PowerShell), I receive the following output:

<p> b'\'"g++ BubbleSort.cpp"\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n' </p>

This only occurs when using the python script. My PATH is set up correctly, as the commands work fine when calling them through the CLI alone. Any help or insight would be greatly appreciated.

标签: pythonc++python-3.xcommand-line-interfacemingw

解决方案


推荐阅读