473,503 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do you know if open failed?

f = open('myfile.bin', 'rb')

How do I know if there was an error opening my file?

Sep 28 '06 #1
5 24836
SpreadTooThin wrote:
f = open('myfile.bin', 'rb')

How do I know if there was an error opening my file?
try:
open('noexist')
except:
print "Didn't open"

--
Posted via a free Usenet account from http://www.teranews.com

Sep 28 '06 #2
SpreadTooThin wrote:
f = open('myfile.bin', 'rb')

How do I know if there was an error opening my file?
you'll notice:
>>f = open("myfile.bin", "rb")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'myfile.bin'
>>>
</F>

Sep 28 '06 #3
On 2006-09-28, SpreadTooThin <bj********@gmail.comwrote:
f = open('myfile.bin', 'rb')

How do I know if there was an error opening my file?
Try it an see.

Seriously, it will raise an exception that you can catch.

try:
f = open('myfile.bin', 'rb')
# Do stuff with f
except IOError, inst:
print 'Phooey.', inst.errno, inst.strerror

--
Neil Cerutti
Sep 28 '06 #4

"tobiah" <to**@tobiah.orgwrote in message
news:45***********************@free.teranews.com.. .
SpreadTooThin wrote:
f = open('myfile.bin', 'rb')

How do I know if there was an error opening my file?
try:
open('noexist')
except:
print "Didn't open"
That's a way to trap any exception. I think a better answer to the
question is "You'll know if it didn't work because Python throws exceptions
when it runs into problems." You can catch exceptions and try to do
something about them if you want to. Uncaught exceptions cause the
interpreter to exit with a stack trace. Sometimes that's the most logical
thing to do.
>>fd = open('doesnt_exist', 'rb')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory: 'doesnt_exist'
It would throw a different exception if there were a permission problem, for
example.

-ej
Sep 28 '06 #5
tobiah a écrit :
SpreadTooThin wrote:
>f = open('myfile.bin', 'rb')

How do I know if there was an error opening my file?
try:
open('noexist')
except:
print "Didn't open"
Should be:

try:
f = open('noexists')
except IOError, e:
print >sys.stderr, "Failed to open 'noexists' : %s" % e

Sep 28 '06 #6

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

Similar topics

6
2669
by: ruben | last post by:
Hi: I'm running a large database on PostgreSQL 7.1.3. 20 days ago the database failed with a threatening and not too descriptive error like: pg_exec() query failed: server closed the...
2
2860
by: dennist685 | last post by:
Can't open Northwind I remembered doing walkthrough using Northwind. However it wasn't an http project but a file project using the development server. Northwind allowed me to add, edit and...
10
2343
by: Gunnar G | last post by:
I'm having problem reading from the beginning of a file. Here is the code (more or less) ifstream codefin; ofstream codefout; while (not_annoyed)
0
12002
by: bazzer | last post by:
hey, im trying to access a microsoft access database from an ASP.NET web application in visual basic 2003.NET. i get the following error when i try running it: Server Error in...
1
4670
by: kencana | last post by:
Hi all, I was wondering why I always get "failed to open stream: HTTP request failed!" error in either loading a normal or xml file. i don't understand why i can't get the whole result. the result...
11
14460
by: alnoir | last post by:
I'm using the Open Perl IDE to write perl scripts. Everytime I try to compile a script Open Perl will open another script called 'perl5db.pl' and find a compiling error in it. The result in the...
6
62648
by: Andy2500 | last post by:
Hi, I'd like to upload an image to a folder, then I have 3 diffrents examples but all of them give an error "failed to open stream: Permission denied", althrough the C:\Inetpub\wwwroot is not...
2
2470
by: swethak | last post by:
Hi, when i run my code it gives error as fopen(lib/providers//provider.RVLogic.php): failed to open stream: Permission denied in F:\Facebook\furniture11\Data...
5
9078
by: tyakimov | last post by:
Hi guys I got a problem On my IIS server I changed the password for the Internet Guest Account 'IUSR_Machine' and suddenly the PHP part of the intranet stopped working. - '... failed to open...
0
7202
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
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
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
5578
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
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.