gpt4 book ai didi

python - 从子进程调用时,G++ 无法解析路径

转载 作者:太空宇宙 更新时间:2023-11-04 10:39:33 24 4
gpt4 key购买 nike

我正在编写一个小的 python 脚本来自动评估学生作业。我有一个骨架,其中提交的作业被解压缩到一个临时文件夹中,我的脚本被调用,并以临时文件夹的路径作为参数。

我的脚本首先复制一些我需要的额外文件,然后尝试大致像这样编译 C++ 代码:

compilation_files = " ".join(glob.iglob("*.cpp"))
compilation_call = ["g++", "-std=c++14", compilation_files, "-o " + output_name]
subprocess.call(compilation_call)

g++ 退出并出现此错误:

g++: error: tests-main.cpp tests-small1.cpp small1.cpp: No such file or directory
g++: fatal error: no input files

我确定文件存在(毕竟,glob 否则无法找到它们),并且子进程在正确的目录中执行。

这是我做的

print "ls: ", subprocess.check_output("ls")
print "pwd: ", subprocess.check_output("pwd")
print "files: ", compilation_files

验证文件确实存在,被正确调用并且脚本在正确的目录中运行。

--- 编辑---

我可以通过传递 shell=True 使脚本工作,但这也意味着手动转义 shell 的文件名。

最佳答案

嗯,您正在调用带有参数列表的子进程。第一个是将要执行的命令的名称,接下来是命令的参数一次一个

所以你正在执行类似的东西:

g++ -std=c++14 "tests-main.cpp tests-small1.cpp small1.cpp" -o xxx

错误提示文件(单数 file 不是复数 files)"tests-main.cpp tests-small1.cpp small1.cpp" 不存在。

你应该这样做:

compilation_call = ["g++", "-std=c++14"] + glob.iglob("*.cpp") + [ "-o " + output_name]

将每个单独的文件名放在其自己的参数列表项中。

关于python - 从子进程调用时,G++ 无法解析路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35746989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com