473,651 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do you know if open failed?

f = open('myfile.bi n', 'rb')

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

Sep 28 '06 #1
5 24861
SpreadTooThin wrote:
f = open('myfile.bi n', '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.bi n', 'rb')

How do I know if there was an error opening my file?
you'll notice:
>>f = open("myfile.bi n", "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********@gma il.comwrote:
f = open('myfile.bi n', '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.bi n', 'rb')
# Do stuff with f
except IOError, inst:
print 'Phooey.', inst.errno, inst.strerror

--
Neil Cerutti
Sep 28 '06 #4

"tobiah" <to**@tobiah.or gwrote in message
news:45******** *************** @free.teranews. com...
SpreadTooThin wrote:
f = open('myfile.bi n', '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_ex ist', '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.bi n', '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
2678
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 connection unexpectedlyThis probably means the server terminated abnormally before or while processing the request. I lost some data and had to recreate a table, without knowing the reason
2
2872
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 insert, and the web page looked fine. I opened a simple project just to see whether I could get my sqlserver on the internet. (vs studio 2005, sqlserver 2005)
10
2371
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
12031
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 '/CinemaBookingSystem' Application. -------------------------------------------------------------------------------- ERROR General error Unable to open registry key 'Temporary (volatile) Jet DSN for process
1
4689
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 i get is only some part of the respective url (the one i tried to load). The following is my php code to open the file: $file=...
11
14472
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 'Error Output' box is: Win32 does not define $Win32::VERSION--version check failed at C:\Open_Perl_IDE_1.0.11.409/perl5db.pl line 438. BEGIN failed--compilation aborted. Compilation failed in require at C:/Perl/lib/Win32.pm line 8. BEGIN...
6
62723
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 protected. Any suggestions would be helpful, thank you for your time.
2
2479
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 Mining\public_html\lib\inc\functions.lib.php Line 673 : trigger_error("Failed to open stream to $filename. Permission denied!", E_USER_ERROR); could you plz tell the solution for how to avoid that errors
5
9090
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 stream: HTTP request failed! HTTP/1.1 401 Access Denied in ...' My PHP script is very simple: <?php
0
8792
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8694
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8457
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8571
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7294
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5605
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2696
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 we have to send another system
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.