we possess a txt file we would like to use in my system called animallog1.txt. How perform i open it so that it can become utilized in my system making use of try and except. I understand how to use the file using open function but not really making use of try except. Could someone inform me how it is accomplished
rggodUnfortunately in Python 2.x it is possible for third-party code to raise exceptions that do not inherit from Exception, so in Python 2.x there are some cases where you may have to use a bare except: and manually re-raise the exceptions you don’t want to catch. Terminating with sys.exit might be considered bad form in python: exceptions are the proper way to generate/handle errors. To differentiate application-level exceptions from other python exceptions, we create a specialized class, which inherits from python’s Exception class. Throw it when there is an application-level error, and catch it in the main code.
rggod16033 money badges66 metallic badges1616 bronze badges
shutas off-topic by sashkello, jwodder, Maciej Gol, oefe, ssedanoNov 25 '13 at 21:50
This query shows up to become off-topic. The users who voted to close up gave this particular cause:
- 'Queries inquiring for program code mustshow a minimum understanding of the issue being solved. Include attempted solutions, why they didn't function, and theexpectedresults. See furthermore: Stack Overflow issue checklist' - sashkello, jwodder, Maciej Gol, oefe, ssedano
2 Solutions
You're also very hazy, but I'll try and respond to your issue. The cause you make use of
Ryan SaxeRyan Saxetry
andexcept
can be to catch an error. In opening a file you would obtain anIOError
. Therefore this is certainly how you would open a file as such:8,4541414 platinum badges5252 silver badges9595 bronze badges
Try out Except is definitely not really a technique of carrying out something. It'beds a method of managing errors. Examine out there this page for more info. Fundamentally, if you believe you're heading to get an error, you put the code you believe will create an mistake in the
try
. After that, you put code you'd like to perform if the mistake does occur in theexcept
.For example:
What will be happening right here is definitely that in the try statement, the collection creates a
ValueError
. It'beds caught in the except clause and the lines there are computed instead.So, for you what I would suggest is:
Alex ChumbleyAlex Chumbley1,51544 platinum badges2525 sterling silver badges4444 bronze badges