try-except

schedule task if exception has occurred python

schedule task if exception has occurred python Question: my question is simple as the title suggest try: response = requests.get(URL2) # download the data behind the URL open(zipname, "wb").write(response.content) # Open the response into a new file # extract zip file to specified location with ZipFile(zipname, ‘r’) as zip_file: zip_file.extractall(path=path) os.remove(zipname) # removes the downloaded …

Total answers: 3

How to repeat the input until a special condition is met in Python?

How to repeat the input until a special condition is met in Python? Question: I need to take integer inputs from the user and add them to a set. The number of integers is unknown. The input process will end when the user input is "Done". Here is my code: s = set() print(‘Please type …

Total answers: 5

try-finally in Python 3 generator

try-finally in Python 3 generator Question: I have met a snippet of Python 3 code: def gen(): try: while True: yield 1 finally: print(“stop”) print(next(gen())) After I run it, I thought at first that the output should be: 1 But actually the result is: stop 1 How can this happen? What happened under the hood? …

Total answers: 3

python 3 try-except all with error

python 3 try-except all with error Question: Is it possible to do a try-except catch all that still shows the error without catching every possible exception? I have a case where exceptions will happen once a day every few days in a script running 24/7. I can’t let the script die but they also don’t …

Total answers: 2

Why doesn't this Python keyboard interrupt work? (in PyCharm)

Why doesn't this Python keyboard interrupt work? (in PyCharm) Question: My Python try/except loop does not seem to trigger a keyboard interrupt when Ctrl + C is pressed while debugging my code in PyCharm. (The same issue occurs when using Ctrl + C while running the program, but not in the PyCharm Python console.) My …

Total answers: 9

python try/except/else with recursion

python try/except/else with recursion Question: Python version: 2.7. OS: Windows 10 64-bit. Note: I have found a way around the issue described below that doesn’t use try/except/else statements. I am asking the question below only because I am curious as to why the code behaves the way it does, and if there is a way …

Total answers: 2

Separating except portion of a try/except into a function

Separating except portion of a try/except into a function Question: I have a try/except where I repeat the except portion frequently in my code. This led me to believe that it would be better to separate the except portion into a function. Below is my use case: try: … except api.error.ReadError as e: … except …

Total answers: 4

How to make a variable inside a try/except block public?

How to make a variable inside a try/except block public? Question: How can I make a variable inside the try/except block public? import urllib.request try: url = "http://www.google.com" page = urllib.request.urlopen(url) text = page.read().decode(‘utf8’) except (ValueError, RuntimeError, TypeError, NameError): print("Unable to process your request dude!!") print(text) This code returns an error NameError: name ‘text’ is …

Total answers: 3

Try/except when using Python requests module

Try/except when using Python requests module Question: Doing some API testing and trying to create a function that given an inputted URL it will return the json response, however if a HTTP error is the response an error message will be returned. I was using urllib2 before, but now trying to use requests instead. However …

Total answers: 3