【发布时间】:2016-06-12 13:35:48
【问题描述】:
我正在尝试编写一个 Python 脚本,该脚本通过 subprocess.Popen() 调用 g++.exe 并使用它将 .cpp 文件编译为 .exe。问题是,无论我如何尝试将路径传递给源文件,都会出现以下错误:
g++.exe: 错误: CreateProcess: 没有这样的文件或目录
我的目录结构如下:
D:/Test/test.py
D:/Test/external/mingw64/g++.exe
D:/Test/c/client/client.cpp
我的代码是:
import os, subprocess
class builder():
def __init__(self):
self.gccPath = os.path.abspath("external/mingw64/g++.exe")
self.sourceDir = os.path.abspath("c/client")
self.fileName = "client.cpp"
self.sourceFile = os.path.join(self.sourceDir, self.fileName)
def run(self):
command = [self.gccPath, self.sourceFile , "-o", "client.exe"]
print command
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
n=1
while True:
nextLine = process.stdout.readline()
if nextLine == '' and process.poll() != None:
break
if nextLine != "" and nextLine != None:
print n, nextLine
n=n+1
builder = builder()
builder.run()
只是我尝试通过路径的一些方式:
Command: ["D:\\Test\\external\\mingw64\\g++.exe", "c/client/client.cpp", "-o", "client.exe"]
Command: ["D:\\Test\\external\\mingw64\\g++.exe", "c\\client\\client.cpp", "-o", "client.exe"]
Command: ["D:\\Test\\external\\mingw64\\g++.exe", "D:\\Test\\c\\client\\client.cpp", "-o", "client.exe"]
我也尝试将 cwd 传递给 Popen:
command = [self.gccPath, "client.cpp", "-o", "client.exe"]
process = subprocess.Popen(command, shell=True, cwd=self.sourceDir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
总是同样的错误。我以前用过 Popen 很多次,这通常是一件小事,所以我现在很困惑我做错了什么。
【问题讨论】:
-
@PM2Ring - 但是我没有用那条线加入两个绝对路径,我用一个字符串加入了一个绝对路径。我不太担心路径外观,它们只是出于调试目的而打印出来。如果以及当我得到这个工作时,所有打印都会被删除。