Python Handle File Open Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site syntax for generic except clause in python About Us Learn more about Stack Overflow the company Business Learn more about python print exception message hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join
Python Custom Exception
the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Python's “open()” throws different errors
Is Nested Try Block Possible In Python
for “file not found” - how to handle both exceptions? up vote 29 down vote favorite 6 I have a script where a user is prompted to type a filename (of a file that is to be opened), and if the file doesn't exist in the current directory, the user is prompted again. Here is the short version: file = input("Type filename: ") ... try: name of errors in python fileContent = open(filename, "r") ... except FileNotFoundError: ... When I tested my script on my MacOS X in Python 3.3x it worked perfectly fine when I type the wrong filename on purpose (it executes the suite under "expect"). However, when I wanted to run my code on a Windows computer in Python 3.2x, I get an error that says that "FileNotFoundError" is not defined. So, Python 3.2 on Windows thinks "FileNotFoundError" is a variable and the programs quits with an error. I figured out that Python 3.2 on Windows throws an "IOError" if the input filename is not valid. I tested it on my Linux machine in Python 2.7, and it's also an IOError. My problem is now, that the code with except "FileNotFoundError": won't run on Windows's Python 3.2, but if I change it to except "IOError": it won't work on my Mac anymore. How could I work around it? The only way I can think of is to use just except, which I usually don't want. python python-3.x filenotfoundexception ioerror share|improve this question edited Feb 22 '13 at 19:57 asked Feb 22 '13 at 19:48 user2015601 6 This isn't due to Mac/Wind
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known as
An Exception Can Be In Python
parsing errors, are perhaps the most common kind of complaint you get python filenotfounderror while you are still learning Python: >>> while True print('Hello world') File "
Working with File Objects 6.2.1. Reading Files 6.2.2. Closing Files 6.2.3. Handling I/O Errors 6.2.4. Writing to Files 6.3. Iterating with for Loops http://www.diveintopython.net/file_handling/ 6.4. Using sys.modules 6.5. Working with Directories 6.6. Putting It All Together 6.7. Summary In this chapter, you will dive into exceptions, file objects, for loops, and the http://www.java2s.com/Code/Python/File/HandleFilereadingException.htm os and sys modules. If you've used exceptions in another programming language, you can skim the first section to get a sense of Python's syntax. Be sure to in python tune in again for file handling. 6.1.Handling Exceptions 6.1.1. Using Exceptions For Other Purposes Like many other programming languages, Python has exception handling via try...except blocks. Python uses try...except to handle exceptions and raise to generate them. Java and C++ use try...catch to handle exceptions, and throw to generate them. Exceptions are everywhere in Python. Virtually every clause in python module in the standard Python library uses them, and Python itself will raise them in a lot of different circumstances. You've already seen them repeatedly throughout this book. Accessing a non-existent dictionary key will raise a KeyError exception. Searching a list for a non-existent value will raise a ValueError exception. Calling a non-existent method will raise an AttributeError exception. Referencing a non-existent variable will raise a NameError exception. Mixing datatypes without coercion will raise a TypeError exception. In each of these cases, you were simply playing around in the Python IDE: an error occurred, the exception was printed (depending on your IDE, perhaps in an intentionally jarring shade of red), and that was that. This is called an unhandled exception. When the exception was raised, there was no code to explicitly notice it and deal with it, so it bubbled its way back to the default behavior built in to Python, which is to spit out some debugging information and give up. In the IDE, that's no big deal,
ErrorHandle File reading Exception import sys try: f = open('myfile.txt') s = f.readline() i = int(s.strip()) except IOError, (errno, strerror): print "I/O error(%s): %s" % (errno, strerror) except ValueError: print "Could not convert data to an integer." except: print "Unexpected error:", sys.exc_info()[0] raise Related examples in the same category1.Define function to Open a file 2.Define file open and read in a functionjava2s.com |Email:info at java2s.com|© Demo Source and Support. All rights reserved.