shell command from python script

2014-04-14
2014-05-02
  • Alejandro Jakubi

    I have two scripts, a shell script with a single line of the form:

    executable $DOC

    and a python script that makes some preprocesing transformation on the text file for this executable.

    I would like to "combine" these two tools into a single one. For instance, a python script that makes the shell call at the end. If this one were the best way to do it, how to implement the above shell call in python?

     
    Last edit: Alejandro Jakubi 2014-04-14
  • Yevgen Muntyan

    Yevgen Muntyan - 2014-04-14

    Perhaps make the python script do what it wants to do, save the result to a temp file, and invoke the command on that temp file? Or just make it two independent scripts; if the executable accepts standard input then you can do the good old "python yourscript.py $DOC | executable". In any case make it something that doesn't depend on medit, except for the $DOC part; you don't want to write (and debug!) some complicated stuff in that tiny preferences dialog entry.
    You can use subprocess module to invoke the command from python code: https://docs.python.org/2/library/subprocess.html#

     
  • Alejandro Jakubi

    Actually I have these scripts in tool files. No, the executable does not seem to accept standard input. I will study the option of using the subprocess module. Does it require "loading" the module? I guess a line like:

    import os
    

    as I have seen in some contributed tool code.

     
    Last edit: Alejandro Jakubi 2014-04-15
  • Alejandro Jakubi

    I am experimenting with the subprocess module. For instance these lines in the python console open the executable:

    >>> import subprocess
    >>> subprocess.call("m18",shell=True)
    

    where m18 is a script in the path that opens the executable (passing it arguments). The problem now is how to pass the $DOC. As I would have expected, this way does not work:

    >>> subprocess.call("m18"+" $DOC",shell=True)
    

    Meaning that the executable opens as if no file argument was passed. I understand it as $DOC not being replaced within the string. Certainly, using the explicit qualified file name instead, does work.

    So, how to pass $DOC so that it is replaced by the file name?

     
  • Yevgen Muntyan

    Yevgen Muntyan - 2014-04-30

    In python, $DOC becomes doc, and it's a document object (moo.Edit). You want something like

    subprocess.call(["m18", doc.get_filename()],shell=True)

     
  • Alejandro Jakubi

    OK, thank you. It works in this variation:

    subprocess.call("m18 "+ doc.get_filename(),shell=True)
    

    But not in the bracket form. I guess that there is a difference in the way the statement is parsed.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks