Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am coding a personal assitant in Python. At this moment, I am planning all the things I am going to do but I have come up with a problem that I can't solve.
I will be running a main script that will check if user says 'Hello' every 3 seconds. If he does so, then it should start running another script/function and stop the current one. After the task is performed it should start running again the main script (I will be using different scripts for each task to make it cleaner). I had thought about a while loop but I am not sure if this is the best option.
The select system call is the a very efficient way to wait until a file is ready to be read before doing something:
import select
import sys
while True:
reads, _, _ = select.select([sys.stdin], [], [], 3)
if reads:
line = reads[0].readline()
if line.strip().lower() == "hello":
# do a thing
print("hi")
Once hello is read, and your function or process is executed, your program will return to reading stdin.
Note that this works for POSIX systems but not for Windows (except for sockets).
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I often debug Python using a Sublime Python repl extension, and the processes often stack up without closing. Is there something I can append to the end of my code to automatically end the repl process? I can right click it to 'kill repl,' but am looking for an automatic way.
I have tried using exit() and sys.exit() but they do not terminate the processes. Frankly I'm not remotely aware of how REPL or terminals in general work under the hood.
I'm not too familiar with sublime text, but from brief searches online on the most prominent REPL (https://packagecontrol.io/packages/SublimeREPL) it looks like the REPL is part of the sublime process which might be why exit() and sys.exit() aren't working. It looks like you can just close the window tab and that ends the process but i'm not 100% sure.
Although it might not be a solution to your specific issue (I can't comment on SO yet), if you are looking for an interactive local REPL for debugging and testing a good option might be ptpython. It has autocomplete and you can use exit() since it's actually a separate python process, but it does require a separate terminal (unless there's a sublime integration).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
So its for a python script, no idea where to start for it lol. the script i'm using sometimes randomly stops and i need the batch file to automatically re run it whenever it stops.
I guess i need to make something with like fdos so like, i run the script, and lets say the batch file every 1 or 2 hours it restarts itself so i can always have it running without worrying about it stopping randomly.
Please help <3.
You could specify an atexit handler in your python script:
import atexit
import os
def exit_handler():
os.system("C:/Path/To/batch.bat")
atexit.register(exit_handler)
Where test.bat contains:
Python C:/Path/To/python.py
However, this will also cause the script to start up again if it exits legitimately.
Perhaps if you could identify what exit code is sent when it exits "randomly", you can check for that exitcode in atexit and start the batch file up if it matches your problematic exit code.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am new to multi-threaded programming in python.
Can someone tell me whether os.system("ls") in python and exec("ls") call in "C" are doing the same ?
Please tell me about the similarities and dissimilarities as well.
In C, exec(whatever) replaces the current process's code with the code from whatever. Thus, it never returns. You can do the same in Python with os.execv and friends -- see https://docs.python.org/2/library/os.html#process-management .
os.system(whatever), on the other hands, forks the current process, execs whatever in the subprocess, waits for it to end, then returns. So, it's the same as system(whatever) is in C: a simple layer on top of fork, exec, and wait system calls (in Unix-like systems; simulated by other means in non-Unix-based systems, of which I believe the only one around in substantial numbers these days is Microsoft Windows).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to write a python code to run the job of the following Shell script for me:
for run in {1..100}
do
/home/Example.R
done
The shell script is basically running an R script for 100 times. Since I am new to python can somebody help me to write this code in python?
You could use subprocess.call to make an external command:
from subprocess import call
for i in xrange(100):
call(["/home/Example.R"])
You can use python's commands module to execute external commands and capture their output.
The module has a function commands.getstatusoutput(cmd), where cmd is the command you are looking to run, as a string.
Something like this could do the trick:
import commands
for x in xrange(100):
commands.getstatusoutput("/home/Example.R")
Each iteration of the for loop, the commands.getstatusoutput() function will even return a tuple (status, output), where status is the status after executing the program, and output being anything that the command would have written to stdout.
Hope that helps.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I am creating a python script that I want to make it run in a loop that outputs numbers but how do I make python stop itself?
Basically my question is how do I make python use Ctrl+C or something else and stop itself?
If this is what you're saying, here is an example:
while True:
#do your number things here
if the_number == some_amount:
break
You use the break command when you reach a certain condition to break out of a while or for loop.
Hopefully this is what you wanted. If you have questions, ask away in the comments.
You can try sending the Control-C signal through os.kill().
http://docs.python.org/2/library/os.html
This is for sending the Control C singal to the process and not for ending a loop (Remolten's Solution).