[英]python sub-process
I usually execute a Fortran file in Linux (manually) as: 我通常在Linux(手动)中执行Fortran文件:
ifort xxx.for -o xxx && ./xxx
(where 'xxx.for' is my Fortran file and 'xxx' is Fortran executable file) ifort xxx.for -o xxx && ./xxx
(其中'xxx.for'是我的Fortran文件,'xxx'是Fortran可执行文件) But I need to call my fortran file (xxx.for) from python (I'm a beginner), so I used subprocess
with the following command: 但我需要调用蟒蛇(我是初学者)我的Fortran文件(xxx.for),所以我用subprocess
用下面的命令:
cmd = ["ssh", sshConnect, "cd %s;"%(workDir), Fortrancmd %s jobname "%s -o %s" exeFilename "%s && %s ./ %s%s"%(exeFilename)]
But I get an error, and I'm not sure what's wrong. 但我得到一个错误,我不确定是什么问题。 Here's the full code: 这是完整的代码:
import string
import subprocess as subProc
from subprocess import Popen as ProcOpen
from subprocess import PIPE
import numpy
import subprocess
userID = "pear"
serverName = "say4"
workDir = "/home/pear/2/W/fortran/"
Fortrancmd = "ifort"
jobname = "rad.for"
exeFilename = "rad"
sshConnect=userID+"@"+servername
cmd=["ssh", sshConnect, "cd %s;"%(workDir), Fortrancmd %s jobname "%s -o %s" exeFilename "%s && %s ./ %s%s"%(exeFilename)]
**#command to execute fortran files in Linux
**#ifort <filename>.for -o <filename> && ./<filename> (press enter)
**#example:ifort xxx.for -o xxx && ./xxx (press enter)
print cmd
How can I write a python program that performs all 3 steps described above and avoids the error I'm getting? 如何编写一个执行上述所有3个步骤的python程序并避免我得到的错误?
there are some syntax errors... 有一些语法错误......
original: 原版的:
cmd=["ssh", sshConnect, "cd %s;"%(workDir), Fortrancmd %s jobname "%s -o %s" exeFilename "%s && %s ./ %s%s"%(exeFilename)]
I think you mean: 我想你的意思是:
cmd = [
"ssh",
sshConnect,
"cd %s;" % (workDir,),
"%s %s -o %s && ./%s" % (Fortrancmd, jobname, exeFilename, exeFilename)
]
A few notes: 几点说明:
PS - For readability it is often a good idea to break long lists into multiple lines :) PS - 为了便于阅读,将长列表分成多行通常是个好主意:)
I would recommend looking at this stackoverflow thread for ssh instead of using subprocess 我建议查看此stackoverflow线程的ssh而不是使用子进程
For the manual part you may want to look into pexpect or for windows wexpect . 对于手动部分,您可能需要查看pexpect或windows wexpect 。 These allow you to perform subprocesses and pass input under interactive conditions. 这些允许您在交互条件下执行子进程并传递输入。
However most of what you're doing sounds like it would work well in a shell script. 但是,你所做的大部分内容听起来都像是在shell脚本中运行良好。 For simplicity, you could make a shell script on the server side for your server side operations, and then plug in the path in the ssh statement: 为简单起见,您可以在服务器端为服务器端操作创建一个shell脚本,然后插入ssh语句中的路径:
ssh user@host "/path/to/script.sh"
You could use fabric for steps 1 and 2. This is the basic idea: 你可以使用fabric来完成第1步和第2步。这是基本的想法:
from fabric.api import *
env.hosts = ['host']
dir = '/home/...'
def compile(file):
with cd(dir):
run("ifort %s.for -o %s" %(file,file))
run("./%s > stdout.txt" % file)
fab compile:filename
你运行fab compile:filename
one error: 一个错误:
you have an unquoted %s in your list of args, so your string formatting will fail. 你的args列表中有一个不带引号的%s,因此你的字符串格式化会失败。
Here is a complete example of using the subprocess module to run a remote command via ssh (a simple echo in this case) and grab the results, hope it helps: 下面是一个使用子进程模块通过ssh运行远程命令的完整示例(在这种情况下是一个简单的回声)并获取结果,希望它有所帮助:
>>> import subprocess
>>> proc = subprocess.Popen(("ssh", "remoteuser@host", "echo", "1"), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> stdout, stderr = proc.communicate()
Which in this case returns: ('1\\n', '') 在这种情况下返回:('1 \\ n','')
Note that to get this to work without requiring a password you will likely have to add your local user's public key to ~remoteuser/.ssh/authorized_keys on the remote machine. 请注意,要在不需要密码的情况下使其工作,您可能需要将本地用户的公钥添加到远程计算机上的~remoteuser / .ssh / authorized_keys。
do you have to use python? 你必须使用python吗?
ssh user@host "command" ssh user @ host“command”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.