running shelscript inside python

harijay harijay at gmail.com
Fri Mar 27 07:41:00 CET 2009


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


On Mar 26, 7:06 pm, Albert Hopkins <mar... at letterboxes.org> wrote:
> On Thu, 2009-03-26 at 15:23 -0700, harijay wrote:
> > 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.




More information about the Python-list mailing list