Python subprocess

I usually execute a Fortran file on Linux (manually) as:

  • Server connection
  • Go to the special folder
  • ifort xxx.for -o xxx && ./xxx (where "xxx.for" is my Fortran file and "xxx" is the Fortran executable)

But I need to call my fortran file (xxx.for) from python (I am new), so I used subprocesswith the following command:

cmd = ["ssh", sshConnect, "cd %s;"%(workDir), Fortrancmd %s jobname "%s -o  %s" exeFilename "%s && %s ./ %s%s"%(exeFilename)]

But I get an error message, and I'm not sure what happened. Here is 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 three steps described above and avoids the error I get?

+5
source share
6 answers

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:

  • see (workDir,), ( pars )
  • , fortan .

PS. :)

fooobar.com/questions/48896/...

+3

pexpect wexpect. .

, , , script. script , ssh:

ssh user@host "/path/to/script.sh"
+2

:
% s , .

+1

ssh ( ) , , :

>>> import subprocess
>>> proc = subprocess.Popen(("ssh", "remoteuser@host", "echo", "1"), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> stdout, stderr = proc.communicate()

: ('1\n', '')

, , , , , ~ remoteuser/.ssh/authorized_keys .

+1

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)
  • Create fabfile.py file
  • And you run fab compile:filename
+1
source

Do you need to use python?

ssh user @host "command"

0
source

All Articles