Python and call Matlab script outside of Python with arguments - python

I feel like i am almost there but need the extra push! I am trying to call a MATLAB script from Python (I'm not worried about the output of the MATLAB script - it runs independently). However, i must be able to send an input into the MATLAB script from python.
Right now I have(in python):
myvartoinput = 55
cmd = 'myscript.m'
process = Popen(["matlab", "-nosplash", "-nodesktop", "-r", cmd], shell=True)
process.communicate()
# Also, how do i 'exit' the MATLAB so it doesn't continue to run
I am not sure how to add "myvartoinput" into the 'myscript.m' file. Also, did I call the script correctly? Finally, i would like it to "exit" so it doesn't stay open in the background on my pc. Any help would be appreciated!

I have solved it differently:
import os
import subprocess
myvartoinput = 55
subprocess.call(['matlab', '-wait','-nosplash','-nodesktop','-r','myscript(\'%s\')' %(myvartoinput )])
Arguments:
'-wait' - python waits till Matlab executes script and continues;
'-nodesktop' - only matlab command window is called;
'-nosplash' - suppresses the display of the splash screen;
'-r' - argument after which follows name of a script
More info here: http://se.mathworks.com/help/matlab/matlab_env/startup-options.html

Related

Python subprocess.run C Program not working

I am trying to write the codes to run a C executable using Python.
The C program can be run in the terminal just by calling ./myprogram and it will prompt a selection menu, as shown below:
1. Login
2. Register
Now, using Python and subprocess, I write the following codes:
import subprocess
subprocess.run(["./myprogram"])
The Python program runs but it shows nothing (No errors too!). Any ideas why it is happening?
When I tried:
import subprocess
subprocess.run(["ls"])
All the files in that particular directory are showing. So I assume this is right.
You have to open the subprocess like this:
import subprocess
cmd = subprocess.Popen(['./myprogram'], stdin=subprocess.PIPE)
This means that cmd will have a .stdin you can write to; print by default sends output to your Python script's stdout, which has no connection with the subprocess' stdin. So do that:
cmd.stdin.write('1\n') # tell myprogram to select 1
and then quite probably you should:
cmd.stdin.flush() # don't let your input stay in in-memory-buffers
or
cmd.stdin.close() # if you're done with writing to the subprocess.
PS If your Python script is a long-running process on a *nix system and you notice your subprocess has ended but is still displayed as a Z (zombie) process, please check that answer.
Maybe flush stdout?
print("", flush=True,end="")

How to start bare X server (X -retro &) in Linux OS using Python script?

I am a newbie and trying to automate a Linux-based Test in Python. Please help me.
This is what I have tried. But It does not work. No errors but display shows blank black screen.
subprocess.Popen(['pkill', 'X'])
time.sleep(5)
subprocess.Popen(['X', '-retro', '&']).communicate()
subprocess.Popen(['export', 'DISPLAY=:0']).communicate()
subprocess.Popen(['openbox', '--replace', '&']).communicate()
The export will have no effect as each command is evaluated in an separate shell if a shell is required otherwise only fork and exec will be involved. Pass the environmental variables via the dictionary keyword argument.
Communicate will wait until the program exits. Also & is pointless as Popen does not wait for the program to finish and there is no sense in backgrounding a job in what is not a shell and has no concept or status of such distinction.
subprocess.Popen(['pkill', 'X'])
time.sleep(5)
subprocess.Popen(['X', '-retro'])
import sys
env = dict(sys.environ)
env['DISPLAY']=':0'
subprocess.Popen(['openbox', '--replace'], env=env)
If there is a problem with this it lies with the commands involved and not the Python code.

Python: Spawn New Minimized Shell

I am attempting to to launch a python script from within another python script, but in a minimized console, then return control to the original shell.
I am able to open the required script in a new shell below, but it's not minimized:
#!/usr/bin/env python
import os
import sys
import subprocess
pyTivoPath="c:\pyTivo\pyTivo.py"
print "Testing: Open New Console"
subprocess.Popen([sys.executable, pyTivoPath], creationflags = subprocess.CREATE_NEW_CONSOLE)
print
raw_input("Press Enter to continue...")
Further, I will need to be able to later remotely KILL this shell from the original script, so I suspect I'll need to be explicit in naming the new process. Correct?
Looking for pointers, please. Thanks!
Note: python27 is mandatory for this application. Eventually will also need to work on Mac and Linux.
Do you need to have the other console open? If you now the commands to be sent, then I'd recommend using Popen.communicate(input="Shell commands") and it will automate the process for you.
So you could write something along the lines of:
# Commands to pass into subprocess (each command is separated by a newline)
commands = (
"command1\n" +
"command2\n"
)
# Your process
py_process = subprocess.Popen(*yourprocess_here*, stdin=PIPE, shell=True)
# Feed process the needed input
py_process.communicate(input=commands)
# Terminate when finished
py_process.terminate()
The code above will execute the process you specify and even send commands but it won't open a new console.

Want to resize terminal windows in python, working but not quite right

I'm attempted to resize the terminal window on launch of a python script to ensure the display will be static size. It's working but not quite what I expected. I've tried a few methods:
import sys
sys.stdout.write("\x1b[8;40;120t")
and
import subprocess
subprocess.call(["echo","-e","\x1b[8;40;120t"])
and even just
print "\x1b[8;40;80t"
They all work and resize the real terminal. However, if my terminal is, let's say 25x80 to start, the script starts, it resizes, then exits. It will not execute the rest of the script. I wrapped it in a try/Except and nothing is thrown. Nothing in the syslogs and python -v script.py shows nothing odd. If I execute the script again or at a term size of 40x120 (my target size)..the script runs just fine. Why is exeecuting the ANSI escape exiting python? Furthermore if I run this interactively it works with no issues.
Using Python 2.6.6.
I tried to run the following script, and it "works" (Linux Debian / Python 2.6 / gnome-terminal):
print "\x1b[8;40;80t"
print "ok"
The window is resized and the script execution continue.
If you confirm in your case the program stops after resizing, my guess would be Python received a signal SIGWINCH when the window is resized.
You should try to add a specific signal handler. Something like that:
def resizeHandler(signum, frame):
print "resize-window signal caught"
signal.signal(signal.SIGWINCH, resizeHandler)
You need to put the terminal in cbreak mode for this. Using the term package (easy_install term) this could look like this:
from term import opentty, cbreakmode
with opentty() as tty:
if tty is not None:
with cbreakmode(tty, min=0):
tty.write('\033[8;26;81t');
print 'terminal resized'

How to run and control a commandline program from python?

I have a python script which will give an output file. I need to feed this output file to a command line program. Is there any way I could call the commandline program and control it to process the file in python?
I tried to run this code
import os
import subprocess
import sys
proc = subprocess.Popen(["program.exe"], stdin=subprocess.PIPE)
proc.communicate(input=sys.argv[1]) #here the filename should be entered
proc.communicate(input=sys.argv[2]) #choice 1
proc.communicate(input=sys.argv[3]) #choice 2
is there any way I could enter the input coming from the commandline. And also though the cmd program opens the interface flickers after i run the code.
Thanks.
Note: platform is windows
Have a look at http://docs.python.org/library/subprocess.html. It's the current way to go when starting external programms. There are many examples and you have to check yourself which one fits your needs best.
You could do os.system(somestr) which lets you execute semestr as a command on the command line. However, this has been scrutinized over time for being insecure, etc (will post a link as soon as I find it).
As a result, it has been conventionally replaced with subprocess.popen
Hope this helps
depending on how much control you need, you might find it easier to use pexpect which makes parsing the output of the program rather easy and can also easily be used to talk to the programs stdin. Check out the website, they have some nice examples.
If your target program is expecting the input on STDIN, you can redirect using pipe:
python myfile.py | someprogram
As I just answered another question regarding subprocess, there is a better alternative!
Please have a look at the great library python sh, it is a full-fledged subprocess interface for Python that allows you to call any program as if it were a function, and more important, it's pleasingly pythonic.
Beside redirecting data stream with pipes, you can also process a command line such as:
mycode.py -o outputfile inputfilename.txt
You must import sys
import sys
and in you main function:
ii=1
infile=None
outfile=None
# let's process the command line
while ii < len(sys.argv):
arg = sys.argv[ii]
if arg == '-o':
ii = ii +1
outfile = sys.argv[ii]
else:
infile=arg
ii = ii +1
Of course, you can add some file checking, etc...

Categories

Resources