运行依赖于第一个的多个shell命令(dir_path)

我正在尝试使用subprocess.run构建我的CMake项目,但是代码完成时没有错误,但无法正常工作。

代码是:

import subprocess

git_repo_remvoe_destination = '/home/yaodav/Desktop/'
git_repo_clone_destination = git_repo_remvoe_destination + 'test_clone_git'
test_path = git_repo_clone_destination + "/test/"
cmake_debug_path = test_path + "cmake-debug-build"
cmake_build_command = " cmake -bcmake-build-debug -H. -DCMAKE_BUILD_TYPE=debug -DCMAKE_C_COMPILER=/usr/local/bin/gcc " \
                      "-DCMAKE_CXX_COMPILER=/usr/local/bin/c++ -bcmake-build-debug -H. " \
                      "-DSYTEM_ARCH=x86_64-pc-linux-gnu-gcc-7.4.0"
cmake_link_command = "cmake --build cmake-build-debug --target all"

cmake_command = ['cd '+test_path,cmake_build_command,cmake_link_command]

out = subprocess.run(cmake_command,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,shell=True)

我尝试了this的答案,但是没有用

我该怎么做?

bin_swnswn 回答:运行依赖于第一个的多个shell命令(dir_path)

2期。

首先,您应该为每个命令调用subprocess.run()一次,而不要在列表中放置三个不同的命令。

第二个:cd ...命令仅在一个子进程中更改当前的工作目录,而连续的命令将不再位于同一目录中。

但是有一个简单的解决方案。

subprocess.run有一个cwd参数(https://docs.python.org/2/library/subprocess.html#popen-constructor),可让您指定应该在其中执行子流程的目录。

因此,应该执行以下操作:

out = subprocess.run(cmake_build_command,stdout=subprocess.PIPE,stderr=subprocess.STDOUT,cwd=test_path,shell=True)
out += subprocess.run(cmake_link_command,shell=True)
本文链接:https://www.f2er.com/3167281.html

大家都在问