This question already has an answer here:
这个问题在这里已有答案:
- Command line execution in different folder 2 answers
- 命令行执行不同的文件夹2答案
I use python script to compile a maven project.
我使用python脚本编译maven项目。
If I use
如果我使用
from subprocess import call
call("mvn clean && mvn compile")
I can't define the directory of my maven project and maven tries to compile in directory of Python script.
我无法定义我的maven项目的目录,maven尝试在Python脚本的目录中编译。
Is there any standard way to define execution directory for the shell command?
是否有任何标准方法来定义shell命令的执行目录?
1 个解决方案
#1
7
your command should use
你的命令应该使用
-
shell=True
since it's using 2 commands chained by an exit condition test - shell = True,因为它使用了由退出条件测试链接的2个命令
cwd=your_directory
- CWD = your_directory
- status check
- 状态检查
like this:
喜欢这个:
from subprocess import call
status = call("mvn clean && mvn compile",cwd="/users/foo/xxx",shell=True)
Personally I tend to avoid depending on shell=True
. In those cases I tend to decompose both commands and put arguments directly as argument list so if some nasty whitespace slips in it is quoted automatically:
我个人倾向于避免依赖shell = True。在这些情况下,我倾向于分解两个命令并将参数直接作为参数列表,因此如果其中有一些令人讨厌的空格被自动引用:
if call(["mvn","clean"],cwd="/users/foo/xxx") == 0:
status = call(["mvn","compile"],cwd="/users/foo/xxx")
Note that if you want to get hold of the commands output from the python side (without redirecting to a file, which is dirty), you'll need Popen
with stdout=PIPE
instead of call
请注意,如果你想获取从python端输出的命令(没有重定向到一个脏的文件),你需要Popen与stdout = PIPE而不是call
#1
7
your command should use
你的命令应该使用
-
shell=True
since it's using 2 commands chained by an exit condition test - shell = True,因为它使用了由退出条件测试链接的2个命令
cwd=your_directory
- CWD = your_directory
- status check
- 状态检查
like this:
喜欢这个:
from subprocess import call
status = call("mvn clean && mvn compile",cwd="/users/foo/xxx",shell=True)
Personally I tend to avoid depending on shell=True
. In those cases I tend to decompose both commands and put arguments directly as argument list so if some nasty whitespace slips in it is quoted automatically:
我个人倾向于避免依赖shell = True。在这些情况下,我倾向于分解两个命令并将参数直接作为参数列表,因此如果其中有一些令人讨厌的空格被自动引用:
if call(["mvn","clean"],cwd="/users/foo/xxx") == 0:
status = call(["mvn","compile"],cwd="/users/foo/xxx")
Note that if you want to get hold of the commands output from the python side (without redirecting to a file, which is dirty), you'll need Popen
with stdout=PIPE
instead of call
请注意,如果你想获取从python端输出的命令(没有重定向到一个脏的文件),你需要Popen与stdout = PIPE而不是call