try-except

Why does mocking 'open' and returning a FileNotFoundError raise AttributeError: __exit__?

半世苍凉 提交于 2019-12-11 02:26:42
问题 Testing by mocking open with a FileNotFoundError raises AttributeError: __exit__ . Why is this happening and what can I do to fix it? The following code opens a simple text file. If the file is missing it generates a default value. It has been checked by regular running and it appears to be working perfectly. so_main.py import os import so_config def load_savelocation(): path = os.path.join(so_config.ROOT, so_config.SAVELOCATION_FN) savelocation_path = os.path.normpath(path) try: with open

Is there an alternative for sys.exit() in python?

跟風遠走 提交于 2019-12-08 14:20:37
问题 try: x="blaabla" y="nnlfa" if x!=y: sys.exit() else: print("Error!") except Exception: print(Exception) I'm not asking about why it is throwing an error. I know that it raises exceptions.SystemExit . I was wondering if there was another way to exit? 回答1: Some questions like that should really be accompanied by the real intention behind the code. The reason is that some problems should be solved completely differently. In the body of the script, the return can be used to quit the script. From

syntaxError: 'continue' not properly in loop

戏子无情 提交于 2019-12-05 17:54:30
问题 I have been struggling with this error for a while now and there seems to be different opinions regarding why the interpreter complains about the 'continue'. So I would like to provide the erroneous code below. import tweepy import time def writeHandlesToFile(): file = open("dataFile.txt","w") try: list = tweepy.Cursor(tweepy.api.followers,screen_name='someHandle',).items(100000) print "cursor executed" for item in list: file.write(item.screen_name+"\n") except tweepy.error.TweepError as e:

Issue with sending POST requests using the library requests

北城余情 提交于 2019-12-04 05:09:28
问题 import requests while True: try: posting = requests.post(url,json = data,headers,timeout = 3.05) except requests.exceptions.ConnectionError as e: continue # If a read_timeout error occurs, start from the beginning of the loop except requests.exceptions.ReadTimeout as e: continue a link to more code : Multiple accidental POST requests in Python This code is using requests library to perform POST requests indefinitely. I noticed that when try fails multiple of times and the while loop starts

syntaxError: 'continue' not properly in loop

夙愿已清 提交于 2019-12-04 03:19:18
I have been struggling with this error for a while now and there seems to be different opinions regarding why the interpreter complains about the 'continue'. So I would like to provide the erroneous code below. import tweepy import time def writeHandlesToFile(): file = open("dataFile.txt","w") try: list = tweepy.Cursor(tweepy.api.followers,screen_name='someHandle',).items(100000) print "cursor executed" for item in list: file.write(item.screen_name+"\n") except tweepy.error.TweepError as e: print "In the except method" print e time.sleep(3600) continue The reason I am particular on including

very simple code, and getting error C2712, could not understand why

我只是一个虾纸丫 提交于 2019-12-03 14:29:48
I'm having trouble for a while with error C2712: Cannot use __try in functions that require object unwinding , after narrowing the problem, I was left with a very very simple code, and i can not understand why it causes this error. I am using Visual Studio under windows. I am compiling with /EHa (I do not use /EHsc) The reason I use __try/__except and not try/catch is because I want to catch ALL the errors, and do not want the program to crash under any circumstances, including for example division by 0, that try-catch does not catch. #include <string> static struct myStruct { static std:

Python Selenium Webdriver - Try except loop

我们两清 提交于 2019-12-03 06:41:50
I'm trying to automate processes on a webpage that loads frame by frame. I'm trying to set up a try-except loop which executes only after an element is confirmed present. This is the code I've set up: from selenium.common.exceptions import NoSuchElementException while True: try: link = driver.find_element_by_xpath(linkAddress) except NoSuchElementException: time.sleep(2) The above code does not work, while the following naive approach does: time.sleep(2) link = driver.find_element_by_xpath(linkAddress) Is there anything missing in the above try-except loop? I've tried various combinations,

python 3 try-except all with error [duplicate]

萝らか妹 提交于 2019-12-03 04:06:40
问题 This question already has answers here : How to get exception message in Python properly (3 answers) Closed 2 years ago . 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 matter since it retries regardless as long as I try except everything. So while I track down any last rare exceptions I

Datetime module - ValueError try/except won't work python 3

て烟熏妆下的殇ゞ 提交于 2019-12-02 23:35:16
问题 I have a programming homework assignment. Everything went smoothly until I reached a problem using Try/Except. If I type a valid datetime, the program will take it and it will move on, but if I use a valid datetime format, the exception won't react. Here is my code: import datetime import csv def get_stock_name(prompt,mode): while True: try: return open(input(prompt) + ".csv") except FileNotFoundError: print("File not found. Please try again.") except IOError: print("There was an IOError

Datetime module - ValueError try/except won't work python 3

核能气质少年 提交于 2019-12-02 14:24:09
I have a programming homework assignment. Everything went smoothly until I reached a problem using Try/Except. If I type a valid datetime, the program will take it and it will move on, but if I use a valid datetime format, the exception won't react. Here is my code: import datetime import csv def get_stock_name(prompt,mode): while True: try: return open(input(prompt) + ".csv") except FileNotFoundError: print("File not found. Please try again.") except IOError: print("There was an IOError opening the file. Please try again.") def get_stock_date(prompt): while True: try: return (input(prompt))