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 2 years ago.
Improve this question
Hi I want to run make for loop to run infinitely.
for eg.
while True:
try:
script()
except Exception as e:
script()
continue
as below because in For loop I have lists where i want to apply on scripts which run in sequencing and continuous
while True:
try:
for symbol in symbol:
script()
except Exception as e:
for symbol in symbol:
script()
continue
I guess you are trying to run a program even when there is an exception and break at some condition, you do not want to run the loop infinitely I suppose. you can do
While true:
try:
for symbol in symbols:
script(symbol) ' you should break however somewhere
Exception as e:
continue
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
Is the usage of try...except below correct? If not, what do I have to change?
try:
name_file = input('Welcome user, enter the name of the file to be opened: ')
file = open('../' + name_file, 'r')
# print(file.readlines(), end=' ')
except FileNotFoundError as err:
print('File not found :(')
else:
try:
res = int(input('Which representation do you want to use? [1] Adjacency List - [2] Adjacency matrix'))
except ValueError as err:
print('Invalid option')
This is correct syntax for try-except-else and will execute as needed if a user enters a non-int value for the second prompt. However, if a different number is entered, the ValueError will not be thrown.
try:
# Some Code
except:
# Executed if error in the
# try block
else:
# execute if no exception
finally:
# Some code .....(always executed)
https://www.geeksforgeeks.org/python-try-except/#:~:text=Else%20Clause%20In%20python%2C%20you%20can%20also%20use,the%20try%20clause%20does%20not%20raise%20an%20exception.
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 2 years ago.
Improve this question
I have 2 chrome tabs like that:
Tabs
I want to close one of them.I will use python to close.How can I close?
Using psutil module this can be achieved
Given code will kill first instance of process found. (If you want to kill all instance you can remove/comment return statement inside try block)
import psutil
def kill_process_by_name(processName):
# Iterate over all running processes
for proc in psutil.process_iter():
# Get process detail as dictionary
pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
# Check if process name contains the given name string.
try:
if processName.lower() in pinfo['name'].lower():
print(pinfo)
p = psutil.Process(pinfo['pid'])
p.terminate()
return
except Exception as e:
pass
#ignore any exception
return
kill_process_by_name( processName='chrome')
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
Trying to write a code that exits on a pressing CTRL + C anywhere in the code had problems implementing because most of online help refer to signalling and other stuff
this might not be related to what am asking for. Can skip this part and provide a solution
import signal
import time
def sigint_handler(signum, frame):
print 'Stop pressing the CTRL+C!'
signal.signal(signal.SIGINT, sigint_handler)
Objective:
Pressing ctrl + c should just quit the program
You can either do a try/except on a KeyboardInterrupt:
try:
while True:
print 1
except KeyboardInterrupt:
print "test"
Alternatively, if the process itself is killed, you will get a SIGTERM sent by the KILL command:
As you indicated, you can define a handler: signal.signal(signal.SIGTERM, my_signal_term_handler)
Here are a list of all the UNIX signals to consider : http://en.wikipedia.org/wiki/Unix_signal#POSIX_signals. Note that SIGKILL cannot be caught.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i want to ask how to break python while True loop in some point and then start it over again.
while True:
if a==0:
print "ok"
elif a==1:
break
#want to start over again
command = sock.recv(1024)
if blah blah ==blah:
.........
What you're looking for is probably continue
while True:
if a==0:
print "ok"
elif a==1:
continue
# goes back to top of loop
else:
print "ok"
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
Is this possible? I'm doing an bukkit plugin now (in Python, yes :D), and I'm forced to do this within one function, so I can't separate it and call it later... For example, if I have loop that loops through players on server and adds everyone except one player, I want it to finish, and then teleport i.e. "Player1" to random player. At the moment, it teleports "Player1" to random player every time because of for loop... I'll give you just little of code, since It looks messy in preview due to many things that are not involved in problem and could be confusable to you... Here it is:
listica = []
for p1 in org.bukkit.Bukkit.getWorld(nextvalue).getPlayers():
if p1.getName() not in listica:
try:
listica.remove(event.getPlayer().getName())
randomtarget = choice(listica)
randomtargetreal = org.bukkit.Bukkit.getPlayer(randomtarget)
event.getPlayer().teleport(randomtargetreal)
event.getPlayer().sendMessage("%sYou teleported to: %s%s"% (bukkit.ChatColor.GREEN, bukkit.ChatColor.DARK_GREEN, randomtarget))
except ValueError:
randomtarget = choice(listica)
randomtargetreal = org.bukkit.Bukkit.getPlayer(randomtarget)
if event.getPlayer().getLocation() != randomtargetreal.getLocation():
event.getPlayer().teleport(randomtargetreal)
event.getPlayer().sendMessage("%sYou teleported to: %s%s"%(bukkit.ChatColor.GREEN, bukkit.ChatColor.DARK_GREEN, randomtarget))
What I want is:
run for loop:
when there is no more players to add a.k.a it finishes
do try loop
P.S. I can't do it in separate function.
Thanks in advance! :)
Do you mean:
def func(args):
for item in loop:
do something
try: # note indentation
something else