473,320 Members | 2,098 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Catching a specific IO error

Hi group :)

I have this standard line:

export = open(self.exportFileName , 'w')

'exportFileName' is a full path given by the user. If the user gives an
illegal path or filename the following exception is raised:
"IOError: [Errno 2] No such file or directory: /some/path/file.txt"

So at the moment I do this:

try:
export = open(self.exportFileName , 'w')
export.write("Something")
export.close()
except IOError:
# calling an error handling method.

Now, this works but of course it catches every IOError, and I can not
figure out how to restrict it to only catch the "[Errno 2]"?

Thanks
Tina

Apr 24 '07 #1
7 13193
Tina I schrieb:
Now, this works but of course it catches every IOError, and I can not
figure out how to restrict it to only catch the "[Errno 2]"?
There's an example that uses the error number:
http://docs.python.org/tut/node10.ht...00000000000000

Thomas
--
sinature: http://nospam.nowire.org/signature_usenet.png
Apr 24 '07 #2
En Tue, 24 Apr 2007 08:44:05 -0300, Tina I <ti*****@bestemselv.com>
escribió:
Hi group :)

I have this standard line:

export = open(self.exportFileName , 'w')

'exportFileName' is a full path given by the user. If the user gives an
illegal path or filename the following exception is raised:
"IOError: [Errno 2] No such file or directory: /some/path/file.txt"

So at the moment I do this:

try:
export = open(self.exportFileName , 'w')
export.write("Something")
export.close()
except IOError:
# calling an error handling method.

Now, this works but of course it catches every IOError, and I can not
figure out how to restrict it to only catch the "[Errno 2]"?
You can get the 2 as the errno exception attribute. BTW, 2 == errno.ENOENT

try:
export = open(self.exportFileName , 'w')
except IOError, e:
if e.errno==errno.ENOENT:
# handle the "No such file or directory" error
# calling an error handling method.

See http://docs.python.org/lib/module-exceptions.html

--
Gabriel Genellina
Apr 24 '07 #3
Thomas Krüger wrote:
Tina I schrieb:
>Now, this works but of course it catches every IOError, and I can not
figure out how to restrict it to only catch the "[Errno 2]"?

There's an example that uses the error number:
http://docs.python.org/tut/node10.ht...00000000000000
So what you'll need to do is catch all IOError exceptions, then test to
see if you've got (one of) the particular one(s) you are interested in.
If not then you can re-raise the same error with a bare "raise"
statement, and any containing exception handlers will be triggered. If
there are none then you will see the familiar traceback termination message.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 24 '07 #4
Steve Holden wrote:
Thomas Krüger wrote:
>Tina I schrieb:
>>Now, this works but of course it catches every IOError, and I can not
figure out how to restrict it to only catch the "[Errno 2]"?
There's an example that uses the error number:
http://docs.python.org/tut/node10.ht...00000000000000

So what you'll need to do is catch all IOError exceptions, then test to
see if you've got (one of) the particular one(s) you are interested in.
If not then you can re-raise the same error with a bare "raise"
statement, and any containing exception handlers will be triggered. If
there are none then you will see the familiar traceback termination message.

regards
Steve
you could also use some pre-testing of the filename os.path.isfile,
os.path.isdir, os.path.split are good
functions to test file/directory existence. Also to verify that you have
permission to manipulate a file, os.access is a good function.
sph
Apr 24 '07 #5
Steven Howe wrote:
Steve Holden wrote:
>Thomas Krüger wrote:
>>Tina I schrieb:

Now, this works but of course it catches every IOError, and I can not
figure out how to restrict it to only catch the "[Errno 2]"?

There's an example that uses the error number:
http://docs.python.org/tut/node10.ht...00000000000000

So what you'll need to do is catch all IOError exceptions, then test
to see if you've got (one of) the particular one(s) you are interested
in. If not then you can re-raise the same error with a bare "raise"
statement, and any containing exception handlers will be triggered. If
there are none then you will see the familiar traceback termination
message.

regards
Steve
you could also use some pre-testing of the filename os.path.isfile,
os.path.isdir, os.path.split are good
functions to test file/directory existence. Also to verify that you have
permission to manipulate a file, os.access is a good function.
The try first approach is better for at least two reasons:

1) It saves you an extra stat() on the disk, which can be really
important for some filesystems I use :)

2) It is atomic. If os.path.isfile() returns True but the file is
deleted before you open it, you are still going to have to handle the
exception.
--
Michael Hoffman
Apr 24 '07 #6
On 25/04/2007 4:06 AM, Steven Howe wrote:
Steve Holden wrote:
>Thomas Krüger wrote:
>>Tina I schrieb:

Now, this works but of course it catches every IOError, and I can not
figure out how to restrict it to only catch the "[Errno 2]"?

There's an example that uses the error number:
http://docs.python.org/tut/node10.ht...00000000000000

So what you'll need to do is catch all IOError exceptions, then test
to see if you've got (one of) the particular one(s) you are interested
in. If not then you can re-raise the same error with a bare "raise"
statement, and any containing exception handlers will be triggered. If
there are none then you will see the familiar traceback termination
message.

regards
Steve
you could also use some pre-testing of the filename os.path.isfile,
os.path.isdir, os.path.split are good
functions to test file/directory existence.
In general, this is laborious, tedious, and possibly even platform
dependent. Then you still need to wrap the open call in try/accept. Why
bother?

In particular, (1) please explain how os.path.split helps with existence
testing:

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
| >>import os.path
| >>os.path.split(r'no\such\path\nothing.nix')
('no\\such\\path', 'nothing.nix')

(2) please explain why you avoided mentioning os.path.exists.
Apr 25 '07 #7
Gabriel Genellina wrote:
You can get the 2 as the errno exception attribute. BTW, 2 == errno.ENOENT

try:
export = open(self.exportFileName , 'w')
except IOError, e:
if e.errno==errno.ENOENT:
# handle the "No such file or directory" error
# calling an error handling method.

See http://docs.python.org/lib/module-exceptions.html

--Gabriel Genellina
Perfect! Just what I was looking for. Thank you! :)

Tina
Apr 25 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ron Vecchi | last post by:
In the RenderControl() method I have some code which will catch the controls HTML output. But what I'm tring to accomplish is catching only a perticular control and its child controls. The code...
5
by: David Lozzi | last post by:
Howdy, I'm doing some simple database reading and loading severl values into labels for readonly use. Here is my script: Dim selStoreID As String If StoreID.SelectedIndex > -1 Then...
2
by: Madhu | last post by:
Hello All, I am getting the below error message when I am trying to connect from client to remote database server installed on Linux. DB2 UDB ESE database is running on a trail version on Linux...
1
by: Groove | last post by:
Hey guys - I'm sure this is a commonly asked question but here goes. I'm trying to catch the error in my global.asax file and redirect to a error page which will email me the actual error upon...
4
by: Jason Richmeier | last post by:
I am sure this has been asked at least once before but I could not find anything when searching. If I set the value of the ExitCode property to 1066 for a windows service, the text "A service...
6
by: Eric | last post by:
is it possible to catch an assertion error? that is, could the following block of code be made to run? assert (false); catch (...) {}
5
by: JonnyB | last post by:
Hi all, I have a txt box that users enter a number in to create a booking. When a user doesn't enter a value access brings up an error message because the value cant be null which is fine but...
2
by: Brent Halsey | last post by:
I am using the ibm_db2 PECL drive in PHP for connecting to or DB2 database. I created a persistent connection and things seemed to work fine at first. However, after a few tests / connections, I...
4
by: Pool | last post by:
I tried to connect DB2 (Sitting in Unix server at my client location) using Db2 connect V8. I am getting the following error message. I tried all the possible options BUt the error is same.. See each...
1
by: johnyjj2 | last post by:
Hello! I've got web application created for MSSQL and IIS. There are two directories in this application - Scripts and WebSite. In Script there is file CreateDb.sql. I need to run it so that it...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
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...
0
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...
0
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
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
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.