try-except

Python selenium CTRL+C closes chromedriver

北城以北 提交于 2021-02-07 20:27:06
问题 How can I catch CTRL+C (a KeyboardInterrupt) without causing the chromedriver to close. It closes the chromedriver when I run my script.py through cmd. driver = webdriver.Chrome() try: while True: #do stuff with chromedriver except KeyboardInterrupt: print "User pressed CTRL + C" #do other stuff with chromedriver It does catch the KeyboardInterrupt in my script, thus my script continues but the chromedriver also gets it and close itself. EDIT 1: The solution here doesn't work when you run the

python try/except/else with recursion [duplicate]

六月ゝ 毕业季﹏ 提交于 2021-02-07 04:31:04
问题 This question already has answers here : Return in Recursive Function (3 answers) Closed 4 years ago . 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 to do what I am trying to do using try/except/else. I have a file called blah.py , with the following code: import os def

Separating except portion of a try/except into a function

你。 提交于 2021-01-27 17:45:57
问题 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 api.error.APIConnectionError as e: ... except Exception as e: ... How would I go about separating this logic into a function so I can do something as simple as: try: ... except: my_error_handling_function(e) 回答1: Just define the function: def my

What is the best way to write try except in Python?

情到浓时终转凉″ 提交于 2020-12-31 23:47:10
问题 Suppose I have a code snippet as following r = requests.post(url, data=values, files=files) Since this is making a network request, a bunch of exceptions can be thrown from this line. For completeness of the argument, I could also have file reads, sending emails, etc. To encounter for such errors I do try: r = requests.post(url, data=values, files=files) if r.status_code != 200: raise Exception("Could not post to "+ url) except Exception as e: logger.error("Error posting to " + url) There are

Can't catch ValueError in Python

十年热恋 提交于 2020-11-25 04:01:55
问题 I am starting to learn Python, and I wrote a very simple code to practice try/except. Here is the code: a = float(input('num1: ')) b = float(input('num2: ')) try: result = a / b except ValueError as e: print ('error type: ', type (e)) print(result) Whenever I enter a letter as a number, the print in except is working, but the code crashes. ZeroDivisionError & TypeError are working, but ValueError is not. I even put inputs in separate try/except and it is still not working. How can I handle

c++ try-except statement

◇◆丶佛笑我妖孽 提交于 2019-12-30 23:04:24
问题 I came across this article about detecting VMWare or Virtual PC http://www.codeproject.com/KB/system/VmDetect.aspx and I saw that they use some kind of try-except statement. So I looked it up in the MSDN: http://msdn.microsoft.com/en-us/library/s58ftw19%28v=vs.80%29.aspx and I don't understand why would I use a try-except instead of the good old try-catch. does it just give me additional information about the exception? If so, I can use a try-catch when I use the code from the attached

Why is except not catching this error?

强颜欢笑 提交于 2019-12-30 09:28:28
问题 I have a program that simulates dice rolls and compares them to values in a chart (set of String lists). I currently get the value from a TEdit. If the box is empty it raises a EConvertError that should be caught by my Try/Except statement, but it's not. Thoughts and advice? Code below, Delphi 7. try //Shooting if ShootingRadio.Checked then BS := StrToInt(Edit1.Text); Randomize; Roll := RandomRange(1,7); Label3.Caption := IntToStr(Roll); if (Roll < StrToInt(ShootingHitChart[BS-1])) then begin

Why is except not catching this error?

陌路散爱 提交于 2019-12-30 09:28:23
问题 I have a program that simulates dice rolls and compares them to values in a chart (set of String lists). I currently get the value from a TEdit. If the box is empty it raises a EConvertError that should be caught by my Try/Except statement, but it's not. Thoughts and advice? Code below, Delphi 7. try //Shooting if ShootingRadio.Checked then BS := StrToInt(Edit1.Text); Randomize; Roll := RandomRange(1,7); Label3.Caption := IntToStr(Roll); if (Roll < StrToInt(ShootingHitChart[BS-1])) then begin

Ignore missing file while downloading with Python ftplib

*爱你&永不变心* 提交于 2019-12-25 04:24:13
问题 I am trying to download a certain file (named 010010-99999-year.gz) from an FTP server. This same file, but for different years is residing in different FTP directories. For instance: ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-lite/2000/010010-99999-1973.gz ftp://ftp.ncdc.noaa.gov/pub/data/noaa/isd-lite/2001/010010-99999-1974.gz and so on. The picture illustrates one of the directories: The file is not located in all the directories (i.e. all years). In such case I want the script to ignore

Exception AttributeError: “'NoneType' object has no attribute 'path'” in

孤街浪徒 提交于 2019-12-24 00:23:07
问题 I am debugging python code (python2.7.12) as my code works but I get NULL for all variables when streaming tweets into the database. The error I got is: Exception AttributeError: "'NoneType' object has no attribute 'path'" in <function _remove at 0x10068f140> ignored I am assuming this error is from the code below: def put_tweets_in_database(tweets): print "putting tweets in database" errors = 0 count = 0 for tweet in tweets: try: commit_tweet_to_database(tweet, count, len(tweets)) count += 1