我正在写一个小蟒蛇脚本来自动学生作业评估。我有一个骨架,提交的作业被解压缩到一个临时文件夹中,我的脚本被调用,并以临时文件夹的路径作为参数。
我的脚本首先复制了我需要的一些额外文件,然后尝试编译大致如下的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的文件名。
答案 0 :(得分:1)
嗯,你正在用一个参数列表调用一个子进程。第一个是将要执行的命令的名称,其中一个是命令的参数。
所以你正在执行类似的事情:
g++ -std=c++14 "tests-main.cpp tests-small1.cpp small1.cpp" -o xxx
并且错误表明文件(单个文件不是多个文件)"tests-main.cpp tests-small1.cpp small1.cpp"
不存在。
你应该这样做:
compilation_call = ["g++", "-std=c++14"] + glob.iglob("*.cpp") + [ "-o " + output_name]
将每个单独的文件名放在自己的参数列表项中。