cmd console run in background - python

Okay so I'm working to get cmd prompt to run code without the user noticing most likely will be creating a list of prompts that determine which command is run. The closest I've come so far is using subprocess which as far as I can tell won't work. So what I've been able to get subprocess to do is allow the command(s) to be written without the cmd console however, it's just using the python console.
The goal I am aiming for is to have buttons a user will click that based on the gathered information will run a code i.e. no internet check the nic via "ping ::1" <- in cmd(but without the cmd console showing up or rather the user needing to do anything (beyond the prompts) and seeing as little as possible). It would then store that information for the rest of the applications use possibly never showing the information to the user as it is expected they will not understand nor will they know what to do with the information. I do need the information stored so if needed it can be accessed by someone who can make use of the information.
import subprocess
subprocess.run('cmd', shell=True)
from here in the python console you are able to type whatever command.

The subprocess.run already executes a terminal command. You don't need to call the cmd. And, it won't pop a screen.
To better undestand, try this:
import subprocess
subprocess.run('echo "Hello World!"', shell=True)
It outputs:
"Hello World!"
CompletedProcess(args='echo "Hello World!"', returncode=0)
It also has options for capturing the output, but for what you are trying to do, this might do.

Related

Unable to get the "whole" childprocess output when using Popen

I'm trying to use a python script to automate an interactive console application.
(it's called BLEConsole and it's a microsoft store app https://www.microsoft.com/en-us/p/bleconsole/9p5662fdq58v , not sure if that's important. )
So I've been using os.popenX and subprocess.popen to launch and interact with it, and it's working fine... mostly !
inp,outp = os.popen4('BLEConsole.exe')
inp.write('ls\n') # this one work
inp.write('open #01\n') # this one don't generate any output for some reason
inp.write('format hex\n') # and it's working again ??
inp.write('quit\n')
print outp.read()
some commands work and print to stdout but some other don't.
they do work (the bluetooth device connects just fine) but i'm just unable to capture any output other than line breaks when executing this specific command. (yes it's supposed to output stuff, it does when I do it manually)
edit : yes I'm checking both stdout and stderr
Also I'm not able to capture the "BLE:" prompt using this method, I don't really need it but I don't understand why.
Thanks for your help

How to call shell command for a single output when they run in a loop?

I want to use data of the shell command top in a python program,
i am trying to do this with subprocess module,
but, the top command runs in a way that it opens in the terminal with a kind of its screen, which requires the keyboard interrupt to get back to normal terminal.
So when i run command from python shell for getting output of top from linux shell, the scrip just goes in infinite loop, it never ends.
So suggest some ways to get a proper output of such commands like top.
man top is very helpful.
You will find the -b argument for batch mode and -n for the number of iterations. Both together:
top -b -n 1
will give you the desired result.
In a general case, you will have to abort the subprocess after you got the output you want. You can typically do this by closing all connections to it, then upon its next output it will receive a SIGPIPE and typically terminate on its own.
In this particular case you can just call top with option -n 1 to run just one iteration. This will make it terminate after displaying one screen:
import subprocess
output = subprocess.check_output([ 'top', '-n', '1' ])
print output
But beware! This output will have a lot of terminal formatting sequences in it. So parsing it might be a bit difficult.
If you want more output, you should also consider option -b (credit for this goes to #Klaus D.) which makes top not abort after reaching the current screen height.

Python shell issue when using Data Nitro

I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).

How to make a python script launched from subprocess.Popen stay open if an error occurs and closes when it returns normally?

Currently, I'm running this on Windows:
args = ['start', windowname, 'python', '-i', myscript]
subprocess.Popen(args, shell=True)
As you can see, I launch a subprocess running myscript in python's interactive mode. In my case, this means that once the script exits, regardless if it errors out or successfully completes, the window/shell stays open. However, I'd like it so that when myscript errors out, the window will stay open, and when myscript runs successfully, the window will close.
The reason I'm doing this is because I want to see the errors and the output leading up to the errors -- I'd prefer to not use some form of logging because it's easier for me to visually see the windows and outputs.
I don't think I can check the returncode because the process I'm interacting with is start/cmd, rather than python. Please correct me if I'm wrong!
Thanks!
I think the easiest way would be to wrap the whole thing in a small batch script that checks for python's return code (presumably set by you by calling sys.exit() with an appropriate return code).
This stackoverflow question covers how to get the return code (apparently it's in %errorlevel%), and you can keep the cmd window open by executing pause in a batch script.

Python: Redirect output to several consoles?

I have a main program, in which a user can call a sub-process (to download files) several times. Each time, I call aria2c using subprocess, and it will print the progress to stdin. Of course, it is desirable that the user can see the progress of each download seperately.
So the question is how can I redirect the output of each process to a seperate console window?
I'm a bit confused. Using subprocess.Popen(...) should spawn a new command prompt automatically for each call. What is aria2c? Is it a program you had written in python as well? Is it a 3rd party exe that writes to the command prompt window?
I can help you to redirect all the sub-processes output to the main command prompt, so it can be displayed inline.
Also, maybe you can give a little more detail on what is going on first, so I can understand your trouble a bit better.

Categories

Resources