Manogna has Published 66 Answers

How to pass a variable to an exception in Python?

Manogna

Manogna

Updated on 13-Feb-2020 04:50:56

Here we are passing a variable to given exception. We are defining a custom exception ExampleException which is a subclass of base class Exception and also defining the __init__ method. We use a try-except block to raise the exception and pass the variable to the exception as follows.Exampleclass ExampleException(Exception): def ... Read More

How to catch LookupError Exception in Python?

Manogna

Manogna

Updated on 12-Feb-2020 10:54:12

LookupError Exception is the Base class for errors raised when something can’t be found. The base class for the exceptions that are raised when a key or index used on a mapping or sequence is invalid: IndexError, KeyError.An IndexError is raised when a sequence reference is out of range.The given ... Read More

How to catch EnvironmentError Exception in Python?

Manogna

Manogna

Updated on 12-Feb-2020 10:53:26

EnvironmentError is the base class for errors that come from outside of Python (the operating system, filesystem, etc.). EnvironmentError Exception is a subclass of the StandarError class. It is the base class for IOError and OSError exceptions. It is not actually raised unlike its subclass errors like IOError and OSError.Any ... Read More

How to catch TypeError Exception in Python?

Manogna

Manogna

Updated on 12-Feb-2020 10:51:23

TypeErrors are caused by combining the wrong type of objects, or calling a function with the wrong type of object.Exampleimport sys try : ny = 'Statue of Liberty' my_list = [3, 4, 5, 8, 9] print  my_list + ny except TypeError as e: print e print sys.exc_typeOutputcan only concatenate list ... Read More

How to catch IndentationError Exception in python?

Manogna

Manogna

Updated on 12-Feb-2020 10:43:35

A IndentationError occurs any time the parser finds source code that does not follow indentation rules. We can catch it when importing a module, since the module will be compiled on first import. You can't catch it in the same module that contains the try/except block, because with this exception, ... Read More

What does "print >>" do in python?

Manogna

Manogna

Updated on 12-Dec-2019 07:02:49

"print >>" is a syntax to extend the standard 'print' statement so that it can be used to print to any file-like object, instead of the default sys.stdout. So it can be used to print directly to files.ExampleFor example, you open a file called my_file, then you can write to ... Read More

How to open a file in binary mode with Python?

Manogna

Manogna

Updated on 12-Dec-2019 07:00:11

"Binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like JPEGs or GIFs, audio files like MP3s or binary document formats like Word or PDF. In Python, files are opened in text mode by default. To open files ... Read More

How to open a binary file in append mode with Python?

Manogna

Manogna

Updated on 12-Dec-2019 06:58:54

"Binary" files are any files where the format isn't made up of readable characters. Binary files can range from image files like GIFs, audio files like MP3s or binary document formats like Word or PDF. To open files in binary append mode, when specifying a mode, add 'ab' to it.For ... Read More

What are the modules available in Python for converting PDF to text?

Manogna

Manogna

Updated on 11-Dec-2019 06:12:51

You can use the PDFMiner package to convert PDF to text.ExampleYou can use it in the following way:  import sys from cStringIO import StringIO  from pdfminer.pdfpage importPDFPage from pdfminer.pdfinterp importPDFResourceManager, PDFPageInterpreter from pdfminer.layout importLAParams from pdfminer.converter importXMLConverter, HTMLConverter, TextConverter  def pdfparser(data):     fp = file(data, 'rb')     resource_manager = ... Read More

How to raise an exception in Python?

Manogna

Manogna

Updated on 04-Dec-2019 10:07:52

We can force raise an exception using the raise keyword. Here is the syntax for calling the “raise” method.raise [Exception [, args [, traceback]]]where, the Exception is the name of the exception; the optional “args” represents the value of the exception argument.The also optional argument, traceback, is the traceback object ... Read More

Advertisements