python - 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.
解决方案
推荐阅读
- python - 使用 URL 发布推文,通过 API 解析推特(使用 tweepy)
- svg - 在 SVG 中定位图像
- asp.net - 在 asp.net 中运行视频时源无效
- c# - 使用 Razor 慢速渲染的 Web 应用程序
- python - 按自定义函数对列表进行排序
- regex - 修复不跨记录的字段中的不平衡引号
- python - 100 门问题的实施问题
- docker - Docker 使用 docker-compose 让服务在后台运行(树莓派 4 上 docker 容器中的 neo4j)
- facebook-graph-api - Instagram 业务 API 速率限制
- magento2 - 基于会话变量的 Magento 2 目录价格规则