Manogna has Published 7 Articles

How to implement a custom Python Exception with custom message?

Manogna

Manogna

Updated on 13-Feb-2020 05:11:20

277 Views

For the given code above the solution is as followsExampleclass CustomValueError(ValueError): def __init__(self, arg): self.arg = arg try: a = int(input("Enter a number:")) if not 1 < a < 10: raise CustomValueError("Value must be within 1 and 10.") except CustomValueError as e: print("CustomValueError Exception!", e.arg)OutputEnter a number:45 CustomValueError Exception! Value ... Read More

What does close() function do in Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:29:19

315 Views

The function close() closes an open file. For example: f = open('my_file', 'r+') my_file_data = f.read() f.close() The above code opens 'my_file'in read mode then stores the data it reads from my_file in my_file_data and closes the file. When you open a file, the operating system gives a file handle to read/write ... Read More

How do I find the location of Python module sources?

Manogna

Manogna

Updated on 30-Sep-2019 09:02:50

12K+ Views

For a pure python module you can find the location of the source files by looking at the module.__file__. For example,  >>> import mymodule >>> mymodule.__file__ C:/Users/Ayush/mymodule.py  Many built in modules, however, are written in C, and therefore module.__file__ points to a .so file (there is no module.__file__ on Windows), and ... Read More

How do I unload (reload) a Python module?

Manogna

Manogna

Updated on 30-Sep-2019 08:50:27

691 Views

The function reload(moduleName) reloads a previously loaded module (assuming you loaded it with the syntax "importmoduleName" without exiting the script. It is intended for conversational use, where you have edited the source file for a module and want to test it without leaving Python and starting it again. For example, ... Read More

How can I remove the ANSI escape sequences from a string in python?

Manogna

Manogna

Updated on 30-Sep-2019 07:34:57

9K+ Views

You can use regexes to remove the ANSI escape sequences from a string in Python. Simply substitute the escape sequences with an empty string using re.sub(). The regex you can use for removing ANSI escape sequences is: '(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]'.For example, import re def escape_ansi(line):     ansi_escape =re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]')   ... Read More

How can I write a try/except block that catches all Python exceptions?

Manogna

Manogna

Updated on 27-Sep-2019 11:29:24

205 Views

It is a general thumb rule that though you can catch all exceptions using code like below, you shouldn’t:try:     #do_something() except:     print "Exception Caught!"However, this will also catch exceptions like KeyboardInterrupt we may not be interested in. Unless you re-raise the exception right away – we ... Read More

How to raise an exception in one except block and catch it in a later except block in Python?

Manogna

Manogna

Updated on 27-Sep-2019 11:27:53

278 Views

Only a single except clause in a try block is invoked. If you want the exception to be caught higher up then you will need to use nested try blocks.Let us write 2 try...except blocks like this:try: try: 1/0 except ArithmeticError as e: if str(e) == "Zero division": print ("thumbs ... Read More

1
Advertisements