Manogna has Published 40 Articles

How to catch LookupError Exception in Python?

Manogna

Manogna

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

1K+ Views

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

296 Views

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

524 Views

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

974 Views

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

How to open a file in binary mode with Python?

Manogna

Manogna

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

5K+ Views

"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

2K+ Views

"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

187 Views

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

What is difference between raw_input() and input() functions in Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:42:24

220 Views

The function raw_input() presents a prompt to the user (the optional arg of raw_input([arg])), gets input from the user and returns the data input by the user in a string. For example, name = raw_input("What isyour name? ") print "Hello, %s." %nameThis differs from input() in that the latter tries ... Read More

How to open a file in read and write mode with Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:39:18

2K+ Views

To open files in read/write mode, specify 'w+' as the mode. For example, f = open('my_file.txt', 'w+') file_content = f.read() f.write('Hello World') f.close() Above code opens my_file.txt in write mode, stores the file content in file_content variable and rewrites the file to contain "Hello World". You can also use r+ mode ... Read More

How to open a binary file in read and write mode with Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:38:47

2K+ Views

To open binary files in binary read/write mode, specify 'w+b' as the mode(w=write, b=binary). For example, f = open('my_file.mp3', 'w+b') file_content = f.read() f.write(b'Hello') f.close()Above code opens my_file.mp3 in binary read/write mode, stores the file content in file_content variable and rewrites the file to contain "Hello" in binary. You can ... Read More

Advertisements