Close the terminal window after doing something - python

I want to just print some information and call an application e.g. notepad.
from subprocess import call
print("Opening Notepad++")
call([r"C:\Program Files (x86)\Notepad++\notepad++.exe"])
exit()
Problem now is that the terminal window doesn't automatically close. It stays open until I close the notepad window. How can I make the terminal window disappear automatically.

use Popen like so
import subprocess
subprocess.Popen(r'C:\Program Files (x86)\Notepad++\notepad++.exe', \
stdout=subprocess.PIPE, shell=False, creationflags = 0x08000000)

You need to call the notepad command with start COMMAND, like in Linux we use COMMAND & to fork the process into the background. in windows we use the start COMMAND
So you code refactored:
from subprocess import call
print("Opening Notepad++")
call([r"start C:\Program Files (x86)\Notepad++\notepad++.exe"])
exit()
Although note I don't have a windows machine to test on.

You could use pythonw.exe:
pythonw script.py
Or change its extension to pyw e.g. script.pyw and double click on it.
If you do that you should print "Opening Notepad++" to a popup window. See: Python Notification Popup that disappears

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" ")

Setting focus of cursor back to Python

I have a little program that uses TKinter to open a csv.
All works fine.
When the user chooses a file, I want the cursor and the active window to return to the Python shell.
I am using this:
os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
When in IDLE, this works, when the program runs, but when I just double click the .py file and run it in the Python Shell, it says it can't find the path.
Anyone know the path I need?
Thanks,
Further research and this is my solution.
import win32gui as wg
from win32gui import GetWindowText, GetForegroundWindow
#This gets the details of the current window, the one running the program
aw = (GetForegroundWindow())
#Do some stuff..
#This tells the program to set the focus on the captured window
wg.SetForegroundWindow(aw)
I hope this helps anyone else looking for the same thing I was.
:-)
Try this, it refers to the running process via pid so it shouldn't matter exactly how you ran it:
import os
pid = os.getpid()
os.system("""/usr/bin/osascript -e 'tell application "System Events" to set frontmost of (first process whose unix id is %d) to true'""" % pid)

os.popen not working as .exe

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

subprocess window disappear quickly

In python, I use subprocess.Popen() to launch several processes, I want to debug those processes, but the windows of those processes disappeared quickly and I got no chance to see the error message. I would like to know whether there is any way I can stop the window from disappearing or write the contents in the windows to a file so that I can see the error message later.
Thanks in advance!
you can use the stdout and stderr arguments to write the outputs in a file.
example:
with open("log.txt", 'a') as log:
proc = subprocess.Popen(['cmd', 'args'], stdout=log, stderr=log)
In windows, the common way of keeping cmd windows opened after the end of a console process is to use cmd /k
Example : in a cmd window, typing start cmd /k echo foo
opens a new window (per start)
displays the output foo
leave the command window opened

How to start external program from Python script in the foreground?

import os
os.system("notepad macros.txt")
or
from subprocess import Popen
Popen(["notepad", "macros.txt"])
both start notepad in the background. How to start it in the foreground?
You can try to use the start command, maybe the /MAX option will force notepad to be in foreground, otherwise if you can wait untill notepad shutdown, use the /WAIT option/
Popen(["start", "/MAX", "notepad", "macros.txt"], shell=True)

Categories