I know this should be obvious, but how does one raise a specific type of
OSError?
When I attempt to perform a file operation on a non-existent file, I get
an OSError: [Errno 2], but what if I want to raise one of those myself?
Thanks in advance,
-Dave
--
Presenting:
mediocre nebula. 4 10269
To raise a specific error, just find the error that you want to raise,
then give the error a text string to print: ex.
raise IOError("This raises an IO error")
On the stderr output, when the routine hits this line, you will get: raise IOError("This raises an IOError")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: This raises an IOError
Just be sure of the error that you want to raise, since some of them
will do stuff like closing open file descriptors as well.
I do not see the point in doing so (why not just copy+paste that
string?), but the errno (specifically ENOENT) corresponds to the
POSIX.1 error number, and the string "No such file or directory" is
done in C via strerror(ENOENT); (check errno(3) and strerror(3)).
I doubt there is something that does this in the standard library
(just checked, there's an errno module, but it is quite sparse), but a
simple C extension would be trivial to write.
However, the best way is just to copy and paste that text into your
program, I mean, why not?
raise OSError("[Errno 2] No such file or directory")
On 4/21/06, David Hirschfield <da****@ilm.com> wrote: I wasn't clear enough in my original post.
I know how to raise a basic OSError or IOError, but what if I want to raise specifically an "OSError: [Errno 2] No such file or directory"? Somehow it must be possible to raise the error with the correct information to bring up the standard message, but where do I find the right values to give?
Thanks, -Dave alisonken1 wrote: To raise a specific error, just find the error that you want to raise, then give the error a text string to print: ex.
raise IOError("This raises an IO error")
On the stderr output, when the routine hits this line, you will get:
raise IOError("This raises an IOError")
Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: This raises an IOError
Just be sure of the error that you want to raise, since some of them will do stuff like closing open file descriptors as well.
-- Presenting: mediocre nebula.
-- http://mail.python.org/mailman/listinfo/python-list
--
Kelvie
Looking at the Python docs.. I found this: http://docs.python.org/ext/errors.html
"""
Another useful function is PyErr_SetFromErrno(), which only takes an
exception argument and constructs the associated value by inspection
of the global variable errno. The most general function is
PyErr_SetObject(), which takes two object arguments, the exception and
its associated value. You don't need to Py_INCREF() the objects passed
to any of these functions.
"""
So, in a C extension, to raise a a specific OSError...
errno = ENOENT;
PyErr_SetFromErrno(PyExc_OSError);
should work...
On 4/21/06, Kelvie Wong <ke****@ieee.org> wrote: I do not see the point in doing so (why not just copy+paste that string?), but the errno (specifically ENOENT) corresponds to the POSIX.1 error number, and the string "No such file or directory" is done in C via strerror(ENOENT); (check errno(3) and strerror(3)).
I doubt there is something that does this in the standard library (just checked, there's an errno module, but it is quite sparse), but a simple C extension would be trivial to write.
However, the best way is just to copy and paste that text into your program, I mean, why not?
raise OSError("[Errno 2] No such file or directory")
On 4/21/06, David Hirschfield <da****@ilm.com> wrote: I wasn't clear enough in my original post.
I know how to raise a basic OSError or IOError, but what if I want to raise specifically an "OSError: [Errno 2] No such file or directory"? Somehow it must be possible to raise the error with the correct information to bring up the standard message, but where do I find the right values to give?
Thanks, -Dave alisonken1 wrote: To raise a specific error, just find the error that you want to raise, then give the error a text string to print: ex.
raise IOError("This raises an IO error")
On the stderr output, when the routine hits this line, you will get:
raise IOError("This raises an IOError")
Traceback (most recent call last): File "<stdin>", line 1, in ? IOError: This raises an IOError
Just be sure of the error that you want to raise, since some of them will do stuff like closing open file descriptors as well.
-- Presenting: mediocre nebula.
-- http://mail.python.org/mailman/listinfo/python-list
-- Kelvie
--
Kelvie
In article <ma***************************************@python. org>,
David Hirschfield <da****@ilm.com> wrote: When I attempt to perform a file operation on a non-existent file, I get an OSError: [Errno 2], but what if I want to raise one of those myself?
raise OSError(2, "No such file or directory") This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: John J. Lee |
last post by:
Today, I wanted to do something like this:
def nr_files(dirName)
try:
return len(os.listdir(dirName))
except OSError, e:
if e.errno == errno.ENOENT:
return 0
else:
raise
|
by: Mark Harrison |
last post by:
Can somebody loan me a clue as to how to check the errno on
an OSError, as described below?
try:
os.makedirs(d)
except OSError:
# if errno == 17 then silently ignore the error because the
#...
|
by: cider123 |
last post by:
I'm coding a project using the following article as reference:
http://www.codeproject.com/csharp/DynamicPluginManager.asp
In this type of project, plugins are loaded dynamically into a Plugin...
|
by: Dave A |
last post by:
I am developing a somewhat complex component at the moment and coincidently
I am also reading the Framework Design Guidelines book.
After reading the section about event raising I have re-written...
|
by: Alec Wysoker |
last post by:
Using Python 2.3.5 on Windows XP, I occasionally get OSError: Permission denied when calling os.remove(). This can occur with a
file that is not used by any other process on the machine, and is...
|
by: Joram Agten |
last post by:
Please put me in CC
When running the following program I get frequent errors like this one
Exception in thread Thread-4:
Traceback (most recent call last):
File...
|
by: Lingo |
last post by:
Hi all,
I have a problem, i am running a batch file which will connect to oracle database via sqlplus.
If i set whenever Oserror Exit and then try to connect it fails with the error message...
|
by: python |
last post by:
Bad file names, i.e. filenames the OS considers illegal, will cause
functions in the os.path module to raise an error.
Example:
import os.path
print os.path.getsize( 'c:/pytest/*.py' )
On...
|
by: buu |
last post by:
So, let's say that a user enters an event name in text box, and would like
to raise it..
is it possible to call events by their 'name'?
Is it possible to enumerate events inside app. and to check...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |