os.popen not working as .exe - python

When I run this code as a application(.exe) file it returns a blank output window.
import os
r = os.popen('cmd').read()
print(r)
(Output Window)
Could someone fix my code or suggest an altenative?
Edit:
My aim is to run the program as an executable, run the commandin the DOS console and return the output of the command.
Thanks

os.popen:
Open a pipe to or from command. The return value is an open file
object connected to the pipe, which can be read or written depending
on whether mode is 'r' (default) or 'w'.
If you just want to call an app:
os.popen('app_you_want_call.exe')
If you are trying to play interactively with the windows command line, you must do something like:
import subprocess
proc = subprocess.Popen('cmd.exe', stdin=subprocess.PIPE)
proc.stdin.write("YOUR COMMAND HERE")
More about subprocess

Related

Subprocess.Popen to open an .exe terminal program and input to the program

I am trying to open a .csv file using a .exe file. Both the file and program are contained in the same folder. When manually opening the folder I drag the csv file ontop of the exe, it then opens and I press any key to commence the program.
When using the shell I can do what I want using this script
E:
cd ISAAC\SWEM\multiprocess\2000
SWEM_1_2000_multiprocess.exe "seedMIXsim.csv"
<wait for program to initialize>
<press any key>
So far in python3 I have tried several variations of subprocess, the latest using Popen with an input argument of ="h" as a random key should start the program.
proc = subprocess.Popen(
['E://ISAAC//SWEM//multiprocess//2010//SWEM_1_2010_multiprocess.exe', '"E://ISAAC//SWEM//multiprocess//2010//seedMIXsim.csv"'], input="h")
However, when I input any arguments such as stdout or input, the python program will almost immediately finish without doing anything.
Ideally, I would like to open a visible cmd window while running the program as the exe runs in the terminal and shows a progress bar.
I solved this by replacing // with \ and using proc.communicate to wait until the process finishes. This comes down to the specific design of the C program, which exits with "press any key".
def openFile(swemPath):
simFile =(swemPath +'seedMIXsim.csv').replace('//', '\\')
swemFile = f"E:\ISAAC\preSWEMgen\SWEMfiles\SWEM{cfg.hoursPerSimulation}hour.exe"
proc = subprocess.Popen([swemFile, simFile], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
wait = proc.communicate(input=b" ")

Waiting for a prompt on an exe

I have a executable that lets me talk to a temperature controller. When I double-click the exe (SCPI-CLI.exe) it will open up a command window with text "TC_CLI>". I can then type my commands and talk to my controller: eg: TC:COMM:OPEN:SER 8
When I use the subprocess.Popen like this
import subprocess
text = 'tc:comm:open:ser 8'
proc = subprocess.Popen(['C:\\Program Files (x86)\\TC_SCPI\\lib\\SCPI-CLI.exe'],
stdout=subprocess.PIPE,stdin=subprocess.PIPE)
proc.stdin.write(text)
proc.stdin.close()
result = proc.stdout.read()
print(result)
the SCPI-CLI.exe will open up, but will not show me the > prompt. What am I doing wrong here? It will hang at the proc.stdin.write(text).
I am a newbie to sub-process.
You can try to add "\n" to your string, that way you send the enter key.
Also please try to add pipe to stderr also and see if it display any error (or maybe it use stderr instead of stdout for displaing messages)
One more thing, is a good idea to wait for this program to exit before reading the results.
Try this:
import subprocess
import time
import os
text = b'tc:comm:open:ser 8\nexit\n'
proc = subprocess.Popen(['SCPI-CLI.exe'],stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE)
out,err = proc.communicate(text)
print(out.decode())

Python subprocess.call function does not redirect output

I am trying to run a shell script called nn.sh (which constantly runs a Linux command over time), from within a python file. I am using the following piece of code:
from subprocess import call, Popen, PIPE
call(['/bin/sh', 'nn.sh', '172.20.125.44', '10', '>>', 'log.txt'])
This code is supposed to run nn.sh with inputs 172.20.125.44 and 10 and stores the result in the file log.txt. When I run this Python script, it only shows the results of running nn.sh on the screen and it does not save them in the fill log.txt. However, if I type
/bin/sh nn.sh 172.20.125.44 10 >> log.txt
in the command line, it is correctly saving all the data into the file log.txt. Any ideas on what goes wrong?
You can't use >> in subprocess calls, instead use stdout parameter:
with open("log.txt", "at") as log:
call(['/bin/sh', 'nn.sh', '172.20.125.44', '10'], stdout = log)

Capturing console output in Python

I can capture the output of a command line execution, for example
import subprocess
cmd = ['ipconfig']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = proc.communicate()[0]
print output
In the above example it is easy as ipconfig, when run directly on the command line, will print the output on the same console window. However, there can be situations where the command opens a new console window to display all the output. An example of this is running VLC.exe from the command line. The following VLC command will open a new console window to display messages:
vlc.exe -I rc XYZ.avi
I want to know how can I capture the output displayed on this second console window in Python. The above example for ipconfig does not work in this case.
Regards
SS
I think this is really matter of VLC interface. There may be some option to vlc.exe so it doesn't start the console in new window or provides the output otherwise. In general there is nothing that stops me to run a process which runs another process and does not provide its output to caller of my process.
Note that, in your second command, you have multiple arguments (-I, rc, and the file name - XYZ.avi), in such cases, your cmd variable should be a list of - command you want to run , followed by all the arguments for that command:
Try this:
import subprocess
cmd=['vlc.exe', '-I', 'rc', 'XYZ.avi']
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
output = proc.communicate()[0]
print output
If you are not running the script from right directory, you might want to provide absolute paths for both vlc.exe and your XYZ.avi in cmd var

Opening another command line interpreter and typing commands

I use "spim" emulator to emulate the mips architecture. The way it works is that I should first have a "filename.asm" file, I then type "spim" in bash to open the command line interpreter for spim, then I can use the spim commands like loading the file and running it, etc..
I am trying to write a python script that opens the spim command line interpreter and starts typing spim commands in it. Is this possible?
Thanks.
This is going to depend on spim, which I'm not familiar with, but if you can pipe something to it, you can do the same in Python
Check out http://docs.python.org/library/subprocess.html
Something like this will get you started:
proc = subprocess.Popen('spim',shell = True,stdin = subprocess.PIPE)
proc.stdin.write("Hello world")
from subprocess import Popen, PIPE, STDOUT
# Open Pipe to communicate with spim process.
p = Popen(['spim'], stdout=PIPE, stdin=PIPE, stderr=STDOUT, shell=True)
# Write a "step 1" command to spim.
p.stdin.write('step 1\n')
p.stdin.close()
# Get the spim process output.
spim_stdout = p.stdout.read()
print(spim_stdout)

Categories