473,408 Members | 2,113 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,408 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 13195
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.