python-3.3

Can't catch mocked exception because it doesn't inherit BaseException

丶灬走出姿态 提交于 2019-11-27 17:12:58
问题 I'm working on a project that involves connecting to a remote server, waiting for a response, and then performing actions based on that response. We catch a couple of different exceptions, and behave differently depending on which exception is caught. For example: def myMethod(address, timeout=20): try: response = requests.head(address, timeout=timeout) except requests.exceptions.Timeout: # do something special except requests.exceptions.ConnectionError: # do something special except requests

Can't use chrome driver for Selenium

痞子三分冷 提交于 2019-11-27 12:05:33
I'm having trouble using the Chrome driver for Selenium. I have the chromedriver downloaded and saved to C:\Chrome: driver = webdriver.Chrome(executable_path="C:/Chrome/") Using that gives me the following error: Traceback (most recent call last): File "C:\Python33\lib\subprocess.py", line 1105, in _execute_child startupinfo) PermissionError: [WinError 5] Access is denied During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Python33\lib\site-packages\selenium\webdriver\chrome\service.py", line 63, in start self.service_args, env=env,

Removing non numeric characters from a string

删除回忆录丶 提交于 2019-11-27 08:34:10
I have been given the task to remove all non numeric characters including spaces from a either text file or string and then print the new result next to the old characters for example: Before: sd67637 8 After: sd67637 8 = 676378 As i am a beginner i do not know where to start with this task. Please Help The easiest way is with a regexp import re a = 'lkdfhisoe78347834 (())&/&745 ' result = re.sub('[^0-9]','', a) print result >>> '78347834745' Jon Clements Loop over your string, char by char and only include digits: new_string = ''.join(ch for ch in your_string if ch.isdigit()) Or use a regex

Seeking from end of file throwing unsupported exception

安稳与你 提交于 2019-11-27 04:59:49
I have this code snippet and I'm trying to seek backwards from the end of file using python: f=open('D:\SGStat.txt','a'); f.seek(0,2) f.seek(-3,2) This throws the following exception while running: f.seek(-3,2) io.UnsupportedOperation: can't do nonzero end-relative seeks Am i missing something here? jonrsharpe From the documentation for Python 3.2 and up: In text files (those opened without a b in the mode string), only seeks relative to the beginning of the file are allowed (the exception being seeking to the very file end with seek(0, 2) ). Therefore, you can change your program to read: f =

How do I compile my Python 3 app to an .exe? [closed]

时光毁灭记忆、已成空白 提交于 2019-11-27 04:05:01
How do I convert my Python app to a .exe ? I made a program with tkinter and was wondering how to make it possible for others to use. I use Python 3.3. I searched for a bit but could not find anything. cx_Freeze does this but creates a folder with lots of dependencies. py2exe now does this and, with the --bundle-files 0 option, creates just one EXE, which is probably the best solution to your question. UPDATE: After encountering third-party modules that py2exe had trouble "finding", I've moved to pyinstaller as kotlet schabowy suggests below. Both have ample documentation and include .exes you

Writing a .CSV file in Python that works for both Python 2.7+ and Python 3.3+ in Windows

跟風遠走 提交于 2019-11-27 03:51:16
问题 EDIT: I put it in the title, but just realized I didn't mention it in the body. This seems to be specific to Windows. I'm having a hard time writing output using the csv Python module in a script that works with both Python 2.7 and 3.3. First try which works as expected in Python 2.7: with open('test.csv', 'wb') as csv_file: writer = csv.DictWriter(csv_file, ['header1', 'header2']) writer.writeheader() for item in items: writer.writerow(item) However, when that same thing is run in Python 3.3

How to write a dictionary into an existing file?

六月ゝ 毕业季﹏ 提交于 2019-11-27 03:17:04
问题 Let's say I have a dictionary and I want to write it into an existing file. How can I do so without losing anything that could potentially already exist in the file? What I was thinking of is doing the following: def write_report(r, filename): input_filename=open(filename, "a") input_filename.close() for (k,v) in r.items(): input_filename.write(k,v) return filename what I want to make sure is that the filename correctly contains the dictionary. 回答1: You can use json module to read and write

subprocess and Type Str doesnt support the buffer API

空扰寡人 提交于 2019-11-27 03:09:00
问题 I have cmd = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE) for line in cmd.stdout: columns = line.split(' ') print (columns[3]) have error in line 3 Type Str doesnt support the buffer API. What am i doing wrong i am on Python 3.3 回答1: You are reading binary data, not str , so you need to decode the output first. If you set the universal_newlines argument to True , then stdout is automatically decoded using the result of the locale.getpreferredencoding() method (same as for opening

Using Pillow with Python 3

我怕爱的太早我们不能终老 提交于 2019-11-27 01:56:39
问题 I'm not having much luck using Pillow with Python 3.3.2 and I'd be grateful for some help. My problem is that after installing Pillow, I can't import Image. My setup: I've got Linux Mint 16 installed (on an HP Pavilion dv7 laptop). I've got Python 3.3.2+ installed, and it's working fine. I've got Python 2.7.5+ installed, and it's working fine. What I did: I followed the instructions at http://pillow.readthedocs.org/en/latest/index.html to install Pillow v2.4.0 (PIL fork): I started with: ~$

'str' object has no attribute 'decode' in Python3

狂风中的少年 提交于 2019-11-26 21:19:02
问题 I've some problem with "decode" method in python 3.3.4. This is my code: for lines in open('file','r'): decodedLine = lines.decode('ISO-8859-1') line = decodedLine.split('\t') But I can't decode the line for this problem: AttributeError: 'str' object has no attribute 'decode' Do you have any ideas? Thanks 回答1: One encodes strings, and one decodes bytes. You should read bytes from the file and decode them: for lines in open('file','rb'): decodedLine = lines.decode('ISO-8859-1') line =