I want to execute and terminate Windows games from shell. The actual need is to launch games using Python upon receiving specific command. I have tried using the following code to launch game.
import subprocess
subprocess.Popen("D:/Entertainement/Games/NFS1/speed.exe",shell=False)
When I run this code, the game launches and stops without any errors. But the same game runs fine when executed from Windows Explorer. The game doesn't require administrative privileges and runs fine without that.
(Posted on behalf of the OP).
I found a workaround. Instead of launching the executable directly, i created a shortcut and launched it with the following command.
import subprocess
p = subprocess.Popen("C:\Users\devel\Desktop\speed.exe.lnk",shell=True)
p.wait()
print("Program Exited with return code : "+str(p.returncode))
Now everything works fine and my Python program waits for the return code. Thank you all for replying.
Related
In the code below I am trying to create certain groups of apps I can start through typing a certain number in the input field. The problem I am facing is that the apps (in this case Firefox) are closing after the python script is finished with executing. Is there a way to start an .exe file without the program exiting together with the script?
import subprocess
app_group_choice = int(input('Enter app group to open: '))
def Apps_school():
subprocess.call(r"C:\Program Files\Mozilla Firefox\firefox.exe")
print("Ok")
if app_group_choice == 1:
Apps_school()
I could not reproduce the same error. What OS are you using? When I ran the provided code, Firefox remained open after the program ended.
However, it appears that subprocess.Popen() might be the right function to use for this application. It opens the app in a new process and then continues with the code but the lifetime of the app (e.g. Firefox) is not dependent on the script that called it. (https://docs.python.org/3/library/subprocess.html#subprocess.Popen)
Ok, so apparently I had this problem only when I was running my python script from the terminal inside VSCode which maybe had parameters that led to Firefox not staying open. When I ran it straight from cmd or by double clicking It worked perfectly fine.
I am running a python in Windows 10 Command prompt. After the system is kept idle the script stop working. The system also had sleep mode turned off. I need to press "enter" key to make script working again. I tried windows scheduler, however it shows CMD pop up everytime script is executed. Please suggest solution to run the script without any human intervention. I am running the script in cmd with 'schedule' module for running after specific interval.
I have been working on a program that remaps certain keys on a connected keyboard. Everything works when I run the script from either idle or sublime. However, after I use pyinstaller nothing works. It still has the process running when I check task manager but the actual remapping does not work. Here is part of the code.
import keyboard
while True:
keyboard.remap_key ('a','m')
I am running a program which allows me to run terminal commands through my Python code which takes input from the user through the command line. This is the part of the code where I open Google-Chrome
import sys
import os
os.system("google-chrome") #I have Ubuntu 16.04
It opens the browser but the problem is that the terminal on which my python code is running becomes the same as the one where Chrome is running which means that I cannot give further input to my Python code. To solve this problem I need the Chrome to run as a process on a different terminal. I tried using subprocess.call("google-chrome", shell=True) but it did not open it on a new terminal.
How do I make the process run on a different terminal?
can this solve your problem?
os.system('gnome-terminal -x chromium-browser')
Use subprocess.popen("command")
Basically, run the subprocess in the background. & is a shell feature. Use popen instead
I have encountered a problem with my python script. I am using python 3.6.2 I am using the os.system function to execute a bash command. The problem is the script launches the bash command and continues without waiting it to finish, knowing that the script acts correctly in my physical machine, it waits till the bash command is done then continues with the following instructions. This is my script:
import os
os.system("C:/Bash/bashCmd.sh")
print("ok")
I also tried subprocess.call, but it never waits in my vm.
My question is about the source of this issue, then if any suggestions to deal with it!