简体   繁体   English

如何从python脚本执行linux命令

[英]How to execute linux commands from a python script

When I use the linux shell I write module load numeca/open/61_numeca_mpi , I press enter, the program uploads the module licence, and then I write fine and press enter again. 当我使用linux shell时,我写module load numeca/open/61_numeca_mpi ,按Enter键,程序上传模块许可证,然后写得fine ,然后再次按Enter键。 I wrote a Python script which does this. 我写了一个Python脚本来做到这一点。

import os

os.system("module load numeca/open/61_numeca_mpi")
os.system("fine")

It uploads the module licence but then it says 它上传了模块许可证,但随后说

ERROR:105: Unable to locate a modulefile for 'fine'.

Any suggestions? 有什么建议么?

Try using the subprocess module: 尝试使用子流程模块:

import subprocess
p= subprocess.Popen(['module','load numeca/open/61_numeca_mpi"'],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
p.stdin.write('fine')
p.communicate()[0]
p.stdin.close()

尝试在同一外壳中执行两个命令,如下所示:

os.system("module load numeca/open/61_numeca_mpi && fine")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM