tags:

views:

620

answers:

6

Hi, i am beginner in python

I am trying to run a " FORTRAN file from python using subprocess "

I wrote a python program:

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

My problem is: i usually execute a Fortran file in Linux(manually) as,

1, connect to the server

2, go to the specific folder

3, ifort xxx.for -o xxx && ./xxx (press enter) , where 'xxx.for' is my Fortran file & 'xxx' is Fortran executable file

But i required to call my fortran file(xxx.for) from python, so i used subprocess like,

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

i don't know whats the error in this line, please help me.

My question is how to program in python for the manual part described above, so that it performs automatically.

Thank you in advance!

A: 

do you have to use python?

ssh user@host "command"

rev
+2  A: 

For the manual part you may want to look into pexpect or for 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. 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:

ssh user@host "/path/to/script.sh"
Jweede
+1  A: 

one error:
you have an unquoted %s in your list of args, so your string formatting will fail.

Corey Goldberg
+3  A: 

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:

  • a tuple with one element requires a comma at the end of the first argument see (workDir,) to be interpreted as a tuple (vs. simple order-of-operations parens)
  • it is probably easier to contruct your fortan command with a single string format operation

PS - For readability it is often a good idea to break long lists into multiple lines :)

my advice

I would recommend looking at this stackoverflow thread for ssh instead of using subprocess

Jiaaro
A: 

Thank you!(Roseman,Rev,Jweede,Goldberg,Robert) for your valuable replies. I fixed the problem. pear

A: 

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:

>>> 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', '')

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.

mrooney