running shelscript inside python

H

harijay

Hi
I want to run shell scripts of the following kind from inside python
and for some reason either the os.system or the subprocess.call ways
are not working for me .

I am calling a fortran command (f2mtz ) with some keyworded input that
is normally piped (<<) in.
The section of code that deals with the shell script is as follows

script="""HKLIN %s HKLOUT %s <<eof
SYMM %s
CELL %s
format '(3f4.0,f11.2,f8.2,f7.3,4f11.3)'
skipline 0
LABOUT H K L IP FOMS PHIS HLA HLB HLC HLD
CTYPOUT H H H I W P A A A A
END
eof
""" %(options.phs,options.mtzfile,options.symm,cellparams)
import subprocess
subprocess.call(["f2mtz" , script])

Normally I would type the commands above into a shell script and then
execute it
for example a file (tmp.sh)

#!/usr/bin/sh
f2mtz HKLIN 32_4run1.phs HKLOUT 32_4run1.mtz <<eof
SYMM p1
CELL 79.814 102.639 153.627 82.465 75.531 73.450
format '(3f4.0,f11.2,f8.2,f7.3,4f11.3)'
skipline 0
LABOUT H K L IP FOMS PHIS HLA HLB HLC HLD
CTYPOUT H H H I W P A A A A
END
eof

And then run that script
"sh tmp.sh"


When I try subprocess.call(["f2mtz" , script]) or os.system("f2mtz %s"
% script)

It does not seem to work the same way. ANy ideas how to get around
this

Thanks a lot for your help
Hari
 
A

Albert Hopkins

Hi
I want to run shell scripts of the following kind from inside python
and for some reason either the os.system or the subprocess.call ways
are not working for me .

I am calling a fortran command (f2mtz ) with some keyworded input that
is normally piped (<<) in.
The section of code that deals with the shell script is as follows

script="""HKLIN %s HKLOUT %s <<eof
SYMM %s
CELL %s
format '(3f4.0,f11.2,f8.2,f7.3,4f11.3)'
skipline 0
LABOUT H K L IP FOMS PHIS HLA HLB HLC HLD
CTYPOUT H H H I W P A A A A
END
eof
""" %(options.phs,options.mtzfile,options.symm,cellparams)
import subprocess
subprocess.call(["f2mtz" , script])

Normally I would type the commands above into a shell script and then
execute it
for example a file (tmp.sh)

#!/usr/bin/sh
f2mtz HKLIN 32_4run1.phs HKLOUT 32_4run1.mtz <<eof
SYMM p1
CELL 79.814 102.639 153.627 82.465 75.531 73.450
format '(3f4.0,f11.2,f8.2,f7.3,4f11.3)'
skipline 0
LABOUT H K L IP FOMS PHIS HLA HLB HLC HLD
CTYPOUT H H H I W P A A A A
END
eof

And then run that script
"sh tmp.sh"


When I try subprocess.call(["f2mtz" , script]) or os.system("f2mtz %s"
% script)

Ask yourself what you are trying to do:

* Run an executable
* pass arguments to the executable
* Use a string standard input to the call

Then re-read over the subprocess module and it should become clearer to
you.
 
H

harijay

Hello Albert ,
Thanks for your pointers
I did finally get it to work

Here is what worked:
script = """SYMM %s
CELL %s
skipline
LABOUT H K L FP FOM PHIS X
CTYPOUT H H H F W P R
FORMAT '(3f4.0,f11.2,f8.2,f8.1,f8.2)'
END
eof""" %(options.symm,cellparams)
import subprocess
f2mtzargs = ["f2mtz hklin %s hklout %s" %
(options.phs,options.mtzfile),"<<eof"]
a = subprocess.Popen(f2mtzargs,stdin=subprocess.PIPE,shell=True)
a.communicate(input=script)


No what I dont understand is why the following Popen line did not work
in the above program .

f2mtzargs = ["f2mtz hklin %s hklout %s <<eof" %
(options.phs,options.mtzfile)]

If I use this version , I get errors that change from run to run .
Sometimes the error was from the python script and some from the f2mtz
program. So it seems like a buffering or some popen caused effect.

CUrrently I got the correct Popen command by trial and error . I am
hoping to understand subprocess so that I can achieve this in a
smarter way.
Thanks


Hi
I want to run shell scripts of the following kind from inside python
and for some reason either the os.system or the subprocess.call ways
are not working for me .
I am calling a fortran command (f2mtz ) with some keyworded input that
is normally piped (<<)  in.
The section of code that deals with the shell script is as  follows
script="""HKLIN %s HKLOUT %s <<eof
SYMM %s
CELL %s
format '(3f4.0,f11.2,f8.2,f7.3,4f11.3)'
skipline 0
LABOUT H K L IP FOMS PHIS HLA HLB HLC HLD
CTYPOUT H H H I W P A A A A
END
eof
""" %(options.phs,options.mtzfile,options.symm,cellparams)
import subprocess
subprocess.call(["f2mtz" , script])
Normally I would type the commands above into a shell script and then
execute it
for example a file (tmp.sh)
#!/usr/bin/sh
f2mtz HKLIN 32_4run1.phs HKLOUT 32_4run1.mtz <<eof
SYMM p1
CELL 79.814 102.639 153.627 82.465 75.531 73.450
format '(3f4.0,f11.2,f8.2,f7.3,4f11.3)'
skipline 0
LABOUT H K L IP FOMS PHIS HLA HLB HLC HLD
CTYPOUT H H H I W P A A A A
END
eof
And then run that script
"sh tmp.sh"
When I try subprocess.call(["f2mtz" , script]) or os.system("f2mtz %s"
% script)

Ask yourself what you are trying to do:

      * Run an executable
      * pass arguments to the executable
      * Use a string standard input to the call

Then re-read over the subprocess module and it should become clearer to
you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top