I need to obtain the error number from an error that has occurred in Python.
Ex; When trying to transfer a directory via the Paramiko package, an error is caught with this piece of code:
try:
sftp.put(local_path,target_path)
except (IOError,OSError),errno:
print "Error:",errno
For which I get the output,
Error: [Errno 21] Is a directory
I want to utilize the error number to go into some more code to transfer the directory and the directory contents.
Thanks for clarifying your question.
Most Exceptions in Python don't have "error numbers". One exception (no pun intended) are HTTPError exceptions, for example:
import urllib2
try:
page = urllib2.urlopen("some url")
except urllib2.HTTPError, err:
if err.code == 404:
print "Page not found!"
else:
...
Another exception (as noted by bobince) are EnvironmentErrors:
import os
try:
f=open("hello")
except IOError, err:
print err
print err.errno
print err.strerror
print err.filename
outputs
[Errno 2] No such file or directory: 'hello'
2
No such file or directory
hello
If you're talking about errno.h error numbers, you can get them from the errno property on the exception object, but only on EnvironmentError (which includes OSError, IOError and WindowsError).
Specifically on WindowsError you'll also get a winerror property with a Windows-specific error number. (You don't often see one of these though, as Python uses the Win32 API directly relatively rarely.)
There is also the errno package, which allows working with error codes with out having to handle magic numbers in the code. See an example here: Are Python error numbers associated with IOError stable?
Related
I have been trying to capture an exception that returns the message "Error in file!" when reading from a file in Python.
I had managed to catch the error when the file isn't existing, but unfortunately I couldn't manage to capture the error when there is an error in a file.
Any suggestions?
try:
# ...
except IOError:
print("Error opening file!")
except Exception:
print("Error in file!")
raise
You can track the exception to find out what exactly went wrong in the try-block, so you can handle the new error.
In order to do this, just print details of the exception:
try:
....
except Exception as f:
print(f.__class__)
print(str(f))
This will give you the class of the error (e.g. NameError) and the specific error the program encountered. So, you can handle it with an additional except-clause or debug the code.
I am trying this code:
try:
res = subprocess.Popen('bgpq3 -4 {} -m 24 -l {}'.format('MAIyNT-
AS38082','12414'), shell=True,
universal_newlines=True,
stdout=subprocess.PIPE).communicate()[0]
except Exception:
print("Wrong")
#do this code
The output is ?
ERROR:Unable to parse prefix 'MAIyNT-AS38082', af=2 (inet), ret=0
ERROR:Unable to parse prefix MAIyNT-AS38082
ERROR:Unable to add prefix MAIyNT-AS38082 (bad prefix or address-family)
so i am not able to use Error handling!!
Any Idea?
when you write except Exception: you are not catching all the exception: the system exit errors and OS errors (as BaseException, SystemExit, KeyboardInterrupt and GeneratorExit) are excluded.
Most of the exceptions of subprocess are OSError.
Since you did not report the full traceback of the error, I may only assume that you're getting one of these errors, and you can catch them using:
except subprocess.CalledProcessError:
or
except OSError:
as PEP 8 suggest, you should NOT use except: alone, even if it will work in your case.
As a rule of thumb, always catch the exact exception you espect to be raised!
You're only handling errors of type exception here. You only need to use except:. This way you're catching all errors that occur in the code.
try:
#your code
except Exception as e:
#handle the exception
For more information refer to the docs I got from a quick google ;)
How can I catch an error on python 3? I've googled a lot but none of the answers seem to be working. The file open.txt doesn't exist so it should print e.errno.
This is what I tried now:
This is in my defined function
try:
with open(file, 'r') as file:
file = file.read()
return file.encode('UTF-8')
except OSError as e:
print(e.errno)
However I does not print anything when I get this error
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
FileNotFoundError is a subclass of OSError, catch that or the exception itself:
except OSError as e:
Operating System exceptions have been reworked in Python 3.3; IOError has been merged into OSError. See the PEP 3151: Reworking the OS and IO exception hierarchy section in the What's New documentation.
For more details the OS Exceptions section for more information, scroll down for a class hierarchy.
That said, your code should still just work as IOError is now an alias for OSError:
>>> IOError
<class 'OSError'>
Make sure you are placing your exception handler in the correct location. Take a close look at the traceback for the exception to make sure you didn't miss where it is actually being raised. Last but not least, you did restart your Python script, right?
Change your OSError to (IOError, OSError) that should work.
#Thomas Wagenaar
I made a Python 2.7.9 script which downloads some photos from the web using urllib.urlretrieve. I made a simple error dictation using the try and except commands, like so:
try:
urllib.urlretrieve("http://example.com/image.jpg", "1.jpg")
except IOError:
print "Could not connect to 'example.com'!"
However, I realized that IOError could be also raised when no space left on the the harddrive. I want to detect what cause the raise of the IOError (could not connect to example.com/no space left), and display the correct error message.
How can I do so? Thanks!
As zoosuck said, you could differentiate between errors with more than one except-Statement. If you want to differentiate between IOErrors, take a look at the documentation of IOError. It reveals that IOError has an attribute "errno". You can use the errno module (https://docs.python.org/2/library/errno.html) to identify the cause:
import urllib
import errno
try:
urllib.urlretrieve("http://example.com/image.jpg", "1.jpg")
except IOError, e:
if e.errno == errno.ENOSPC:
print "No space left on device"
else:
print "Could not connect to 'example.com'!"
I'm using Fabric to automate, including the task of creating a directory. Here is my fabfile.py:
#!/usr/bin/env python
from fabric.api import *
def init():
try:
local('mkdir ./www')
except ##what exception?##:
#print exception name to put in above
Run fab fabfile.py and f I already have ./www created an error is raised, but I don't know what kind, so I don't know how to handle the error yet. Fabric only prints out the following:
mkdir: cannot create directory ‘./www’: File exists
Fatal error: local() encountered an error (return code 1) while executing 'mkdir ./www'
Aborting.
What I want to do is be able to find out the error type so that I can except my errors properly without blanket statements. It would be really helpful if an answer does not just tell me how to handle a mkdir exception, but print (or otherwise find the name to) any exception I may run into down the line (mkdir is just an example).
Thank you!
The issue is that fabric uses subprocess for doing these sorts of things. If you look at the source code for local you can see it doesn't actually raise an exception. It calls suprocess.Popen and uses communicate() to read stdout and stderr. If there is a non-zero return code then it returns a call to either warn or abort. The default is abort. So, to do what you want, try this:
def init():
with settings(warn_only=True):
local('mkdir ./www')
If you look at the source for abort, it looks like this:
10 def abort(msg):
21 from fabric.state import output
22 if output.aborts:
23 sys.stderr.write("\nFatal error: %s\n" % str(msg))
24 sys.stderr.write("\nAborting.\n")
25 sys.exit(1)
So, the exception would be a SystemExit exception. While you could catch this, the proper way to do it is outlined above using settings.
It is nothing to handle with exception, it is from the fabric api
try to set the entire script's warn_only setting to be true with
env.warn_only = True
Normally, when you get an uncaught exception, Python will print the exception type along with the error message:
>>> raise IOError("Error message.")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: Error message.
If that's not happening, you're probably not getting an exception at all.
If you really want to catch an arbitrary exception and print it, you want to catch Exception or BaseException. BaseException will include even things like KeyboardInterrupt, though, so be careful with that.
def init():
try:
local('mkdir ./www')
except BaseException as e:
print "local() threw a", type(e).__name__
raise # Reraise the exception
In general:
try:
some_code()
except Exception, e:
print 'Hit An Exception', e
raise
Will tell you what the exception was but if you are not planning on actually handling some of the exceptions then simply getting rid of the try: except: lines will have exactly the same effect.
Also if you run your code under a debugger then you can look at the exception(s) that you hit in more detail.
def init():
try:
local('mkdir ./www')
except Exception as e:
print e.__class__.__name__
That's all there is to it!
edit: Just re-read your question and realized that my code would only print "Fatal" in your case. It looks like fabric is throwing an error and returning their own error code so you would have to look at the documentation. I don't have any experience with fabric so I'd suggest to look here if you haven't already. Sorry if this isn't helpful!