Manogna has Published 40 Articles

What does open() function do in Python?

Manogna

Manogna

Updated on 01-Oct-2019 11:30:05

141 Views

The function open() opens a file. You can use it like: 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. The first argument of open is the name of the file and ... Read More

What does close() function do in Python?

Manogna

Manogna

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

231 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

10K+ 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 to retrieve Python module path?

Manogna

Manogna

Updated on 30-Sep-2019 08:51:02

1K+ 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 therefore, ... Read More

How do I unload (reload) a Python module?

Manogna

Manogna

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

426 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 to replace the last occurrence of an expression in a string in Python?

Manogna

Manogna

Updated on 30-Sep-2019 08:25:24

3K+ Views

This problem can be solved by reversing the string, reversing the string to be replaced, replacing the string with reverse of string to be replaced with and finally reversing the string to get the result. You can reverse strings by simple slicing notation - [::-1]. To replace the string you can ... Read More

How to correctly sort a string with a number inside in Python?

Manogna

Manogna

Updated on 30-Sep-2019 08:24:05

2K+ Views

This type of sort in which you want to sort on the basis of numbers within string is called natural sort or human sort. For example, if you have the text:['Hello1', 'Hello12', 'Hello29', 'Hello2', 'Hello17', 'Hello25'] Then you want the sorted list to be:['Hello1', 'Hello2', 'Hello12', 'Hello17', 'Hello25', 'Hello29'] and not:['Hello1', 'Hello12', ... 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

6K+ 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

132 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

200 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

Advertisements