How to run a python program in between another python program
os.system(r'pythonw.exe D:\text.py')
I have tried this but it had pause execution between the main program
and also tried
subprocess.run(r'pythonw.exe D:\text.py')
suppose I have program text.py which notify me every hour
I have main program i.e.
code...
os.system(r'pythonw.exe D:\text.py')
code...
print("hello world")
my command should run text.py in the background and execute the following code.
You can use Python Library named "Soldier". This is very easy to use and implement.
https://pypi.org/project/soldier
import soldier
firefox_process = soldier.run('firefox', background=True)
firefox_process.pid
Related
I'm trying to run one script from another script. I've read; What is the best way to call a script from another script? and I can't seem to get this to work.
My main script (Script A) does a lot of image processing and GUI interactions. However, randomly an error message or other window might appear interrupting the GUI interactions until the message or window is closed.
I've written a second script (Script B) that I want to run perpetually that closes these windows or error messages when discovered.
I'm trying to call Script B from Script A like this:
import close_windows
close_windows.closeWindows
print("Starting Close Windows....")
And Script B is:
import pyautogui as py
def closeWindows():
image = r'C:\image.jpg'
image2 = r'C:\image2.jpg'
while True:
foundimage = py.locateCenterOnScreen(image)
foundimage2 = py.locateCenterOnScreen(image2)
if foundimage or foundimage2 != None:
py.click(1887, 65)
When I run script B independently it works, when I try running it via Script A with close_windows.closeWindows nothing happens.
I've also tried from close_windows import closeWindows and calling closeWindows but again, nothing happens.
I have a python script calling another program with the os.system command. It is a very complex program which can be called in the Terminal using ./myProgram. I want to automatically execute said program and do different stuff (which works fine) in between.
Somehow this works:
print('start')
os.system('ll')
print('end')
But calling the program with the python script:
print('start')
os.system('./myProgram')
print('end')
just executes myProgram without showing the print statements. myProgram itself displays information in the terminal.
Later I want to do more than just print something in between.
I tried using the subprocess module:
print('start')
subprocess.call('./myProgram', shell = True)
print('end')
which shows the same results as the os.system module.
Which properties of a program do not allow my python script to run properly?
And how can I call another program with my python script, execute said program and continue with the script afterwards?
u can always use a safe and secure way to call it
if u want to call app.py in the folder scripts_folder
just write:
from scripts_folder import app
Now if u have a def inside app.py for example my_def_example() u can call it by writing:
app.my_def_example()
all.
Is there a way, using Python, to check if the script that is currently running is requested to close? For example, If I press the X-Button (close program button) on the top-right to close it, or end the script in any other way, can the script do some code before it ends? Example:
# script goes here...
if Script_To_Be_Closed: # replace this with an actual line of code.
do_stuff
There are multiple options you may use, like trapping keyboardinterrupts, but the simplest is atexit, which executes a function whenever a scripts is ended (except of a hard process kill indeed).
import atexit
def my_exit_function(some_argument):
// Your exit code goes here
print(some_argument)
if __name__ == '__main__':
atexit.register(my_exit_function, 'some argument', )
// Your script goes here
You can use a shell script to do the job
You can see the script command shown below which calls itself after executing the command to run the python file. once the python file is closed the next line will force the python command to run again. you can also customise the behaviour the way you want.
main.py
#!/bin/bash
python3 ./main.py
source ./infiniteRun.sh
If you need to stop the job just edit the file and remove the last line source ./infiniteRun.sh and save the file.
I want to start a python script and then automatically close that script after 2 minutes, run another command, and keep doing the same thing again like this (loop) forever :
Cd c:/location.of.script/
pythonscript.py
Stop (like ctrl+c) pythonscript.py after 120s
Del -f cookies.file
.
.
.
Is this even possible with a batch file on windows 10? If so, can someone please help me with this?
I’ve been looking everywhere but found nothing except the exit() command which stops the script from inside - this isn’t what I want to do.
You can change your python script to exit after 2 minutes, and you could batch file that has a while loop that runs forever and run the python script then deletes the cookie.file, I don't know if that's exactly what you want, but you can do it by putting a timer in your python script.
You can make a separate thread that keeps track of the time and terminates the code after some time.
An example of such a code could be:
import threading
def eternity(): # your method goes here
while True:
pass
t=threading.Thread(target=eternity) # create a thread running your function
t.start() # let it run using start (not run!)
t.join(3) # join it, with your timeout in seconds
And this code is copied from https://stackoverflow.com/a/30186772/4561068
I am working on a text based game that I run by double clicking on my top level script namely TopLevel.py.
I am looking for a way to open two terminals in this script. In the one terminal the main game will be run where damage is done and spells are used etc. In the other terminal I would like to display a list of commands that the user can type in , and I want the latter one to stay there and not close until the game is finished. I am not going to show you the whole top level script (it is too long) but this is basically what I want to achieve:
def displayCommands(hero):
list_of_commands = []
#this contains all my commands that the user can type in
def main():
hero = Hero() #make hero instance
enemy = Enemy() #make and enemy instance
a_game = TopLevel(hero,enemy) #create game engine
a_game.play() #start game
#implement code here to open another terminal
#and display user commands in there
Is there a way that I can open another terminal in this script and pass the displayCommands() function as a parameter to display its contents in the second terminal? Any help will be appreciated :)
It's possible for one Python script to spawn another that will run in parallel with it via subprocess. The spawned process can then display any text piped to it (via normal print statements or calls) in a simple tkinter-based window -- see the errorwindow module in this answer of mine for more information.
It likely doesn't matter how the original script gets started. I personally have used it both in ones that were started from a command shell as well as from other tkinter based applications -- so starting yours from powershell should be fine. The module's original author was using Linux or something similar, I believe.
You should convert your second program to .exe first,the one will display user commands. Say you saved it as usercommands.exe, after then in your main script use this to open that;
import subprocess
subprocess.Popen([r"usercommands.exe"],
creationflags=subprocess.CREATE_NEW_CONSOLE)
It'll open another console window that runs usercommands.exe when you run your main file.
Notes;
They must be in the same directory, your main file and .exe file
usercommands.exe must show the commands in an infinite loop(while True), so it's
going to display commands untill the user close it.
Edit;
Now, we have some spells and usercommands will have to use that spell variables after then show us some combinations. For doing this, we have to import your first file to usercommands script.It's going to like this;
usercommands.py
import mainfile #mainfile.py
if choose == mainfile.wizard:
print (something)
if choose == mainfile.subzero:
print (something)
The algorithm should be like this,you will convert usercommands.py to .exe then it will work as you want I guess, we can't now before you try it ;)