Accessing matlab terminal from python - python

How do I call commands in a Matlab terminal when it's opened through Python? I call this:
subprocess.call(["matlab", "-nosplash", "-nodesktop", "-r"], shell=True)
which opens a terminal window. But how can I send new commands in there? I tried simply adding them to the current call, but they don't execute.

This worked for me:
subprocess.call(["matlab", "-nosplash", "-nodesktop", "-r", "command1;command2;"], shell=True, stdin=subprocess.PIPE, stout=subprocess.PIPE)

Related

From Python run WinSCP commands in console

I have to run a few commands of WinSCP from a Python class using subprocess.
The goal is to connect a local Windows machine and a Windows server with no FTP installed and download some files. This is what I tried
python
proc = subprocess.Popen(['WinSCP.exe', '/console', '/WAIT', user:password#ip:folder , '/WAIT','get' ,'*.txt'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
With this I get it to open the WinSCP console and connect to the server, but it doesn't execute the get command. Is the problem because the get is executed on the Windows console and not in the WinSCP console?
I also tried replacing winscp.exe /console for winscp.com /command.
Is there any way to do this?
If you want do without generating a script file, you can use a code like this:
import subprocess
process = subprocess.Popen(
['WinSCP.com', '/ini=nul', '/command',
'open ftp://user:password#example.com', 'get *.txt', 'exit'],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in iter(process.stdout.readline, b''): # replace b'' with '' for Python 2
print(line.decode().rstrip())
The code uses:
/command switch to specify commands on WinSCP command-line;
winscp.com instead of winscp.exe, as winscp.com is a console application, so its output can be read by Python.
Though using the array for the arguments won't work, if there are spaces in command arguments (like file names). Then you will have to format the complete command-line yourself. See Python double quotes in subprocess.Popen aren't working when executing WinSCP scripting.
So when using the /script option you should specify a file containing the batch commands.
To specify all the commands on the command line as you're doing, use the /command option instead of /script.

Python: get stdout from background subprocess

i'm trying to get informations of a network interface on a linux machine with a python script, i.e. 'ifconfig -a eht0'. So i'm using the following code:
import subprocess
proc = subprocess.Popen('ifconfig -a eth0', shell=True, stdout=subprocess.PIPE)
proc.wait()
output = proc.communicate()[0]
Well if I execute the script from terminal with
python myScript.py
or with
python myScript.py &
it works fine, but when it is run from background (launched by crontab) without an active shell, i cannot get the output.
Any idea ?
Thanks
Have you tried to used "screen"?
proc = subprocess.Popen('screen ifconfig -a eth0', shell=True, stdout=subprocess.PIPE)
I'm not sure that it can work or not.
Try proc.stdout.readline() instead of communicate, also stderr=subprocess.STDOUT in subprocess.Popen() might help. Please post the results.
I found a solution to the problem, i guess that the system is not able to recognize the function ifconfig when executed by the crontab. So adding the full path to the subprocess allows the script to be executed properly:
`proc = subprocess.Popen('/sbin/ifconfig -a eth0',shell=True,stdout=subprocess.PIPE)
proc.wait()
output = proc.communicate()[0]`
and now i can manage the output string.
Thanks

Opening a terminal running the same program in Python

I am familiar with how to open a terminal from Python (os.system("gnome-terminal -e 'bash -c \"exec bash\"'")), but is there a way to open another terminal running the same program that opened the new terminal?
For instance, if I was running a program called foo.py and it opened another terminal, the new terminal would also be running foo.py.
See this question, it's pretty close. You want to add sys.argv as a parameter, though:
import sys
import subprocess
cmd = 'xterm -hold -e ./{0}'.format(' '.join(sys.argv))
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Be sure you somehow check how many processes/terminals you run already, otherwise it will hang your machine in a matter of seconds.

How to run a DOS batch file in background using Python?

How to run a DOS batch file in background using Python?
I have a test.bat file in say C:\
Now, I want to run this bat file using python in the background and then I want to return to the python command line.
I run the batch file using subprocess.call('path\to\test.bat') from the python command line.
It runs the batch file in the same window as the python command line.
If still not clear/ TL.DR-
What is happening:
>>>subprocess.call('C:\test.bat')
(Running test.bat. Can't use python in the same window)
What I want:
>>>subprocess.call('C:\test.bat')
(New commandline window created in the background where test.bat runs in parallel.)
>>>
This seems to work for me:
import subprocess
p = subprocess.Popen(r'start cmd /c C:\test.bat', shell=True)
p.wait()
print 'done'
The documentation for subprocess.call says
Run the command described by args. Wait for command to complete, then return the returncode attribute.
In your case you don't want to wait for the command to complete before you continue your program, so use subprocess.Popen instead:
subprocess.Popen('C:\test.bat')
I would use
subprocess.Popen("test.bat", creationflags=subprocess.CREATE_NEW_CONSOLE)
#subprocess.call("ls")
#etc...
So then you can keep calling other commands in the same file, having "test.bat" run on a separate cmd window.

Python/CMD command

So I am running a command in my python py file
myNewShell = os.system('start "%s" /d "%s" cmd /f:on /t:0A /k "W:\\Desktop\\alias.bat"' % (myShot, myWorkDir))
This opens up a shell
How exactly would I input something into this shell directly from my python script, thus bypassing your actual cmd.exe. I have a bunch of DOSKEYs set up, such as maya which opens up the maya program. How would I add a line of code to my python script, so that it loads the shell with those aliases and inputs my command directly
Take a look at the powerful and useful subprocess module
You can then do code like this
import subprocess
pro = subprocess.Popen("cmd", stdout=subprocess.PIPE, stdin=subprocess.PIPE)
pro.stdin.write("mybat.bat\n")
pro.stdin.write("myother.bat\n")
pro.stdin.write("start mysillyprogram\n")
pro.stdin.flush()
pro.terminate() # kill the parent

Categories