I am trying to capture and/or remove the output of a command launched by a os.system() call. The script will run under Linux and Windows.
I cannot use the subprocess module because the said command is interactive (i.e. : the user can type instructions to trigger various actions). So please do not mention this thread as a duplicate of the common questions asked for instance in :
- Python: How to get stdout after running os.system?
- How to store the return value of os.system that it has printed to stdout in python?
- Assign output of os.system to a variable and prevent it from being displayed on the screen
- ...
One solution could be to make subprocess work with such a "wrapped" program, but this seem quite a complicated task, and I want to keep the solution simple (no external module or 1000-line snippet...) as it is not a primary functionality of my script. The following threads seemed promising, but they do not work as well as a crude os.system() (nor are they as simple... ) :
- Running an interactive command from within python
- Non-blocking read on a subprocess.PIPE in python
- http://log.ooz.ie/2013/02/interactive-subprocess-communication-in.html
Another solution could be to work out a "tee" function such as the one natively supported in Linux distribs. I found a good implementation here for example (a Tee class which modifies sys.stdout to write both to a file and to the original sys.stdout) :
- How do I duplicate sys.stdout to a log file in python?
The problem is that os.system() does not seem to print to the main script stdout. Instead, it launches the program in a subshell and I cannot find a way to retrieve/suppress its output...
If you have any other approach or solution, please let met know. Thanks.
Some details about the context were asked and given in the comments below. The main interrogation bears upon why I stick with os.system() when subprocess seems to be the obvious solution.
The program I execute is called CAST3M (http://www-cast3m.cea.fr/). It is a finite-element code used to solve problems in various fields of physics. There is no GUI, so the user interacts via a command-line custom language called GIBIANE. Classically, you can either feed CAST3M with a pre-written GIBIANE data file, or launch the program without a data file an enter commands on-the-fly. Here are typical GIBIANE instructions (they define some points, then a line, a square and finally a cube lying upon them) :
OPTI 'DONN' 3 'ELEM' 'CUB8' ;
PT1 = 0. 0. 0. ;
PT2 = 1. 0. 0. ;
PT3 = 0. 1. 0. ;
PT4 = 0. 0. 1. ;
NN1 = 5 ;
DR1 = PT1 DROI NN1 PT2 ;
SF1 = DR1 TRAN NN1 PT3 ;
VL1 = SF1 VOLU 'TRAN' NN1 PT4 ;
I made a wrapper in Python meant to tweak some functionnalities of CAST3M before actually launching it. I need to log what is printed by this Python script as well as the output of the CAST3M session. When there is no interactivity, subprocess does the job. When there is interactivity, I am forced to use os.system() because subprocess works poorly with CAST3M (I just need to hand it over to CAST3M, which os.system() does out-of-the-box, at the expense of IO control that's true)