Rajendra Dharmkar has Published 452 Articles

How to create a unique temporary file name using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 18-Feb-2020 04:54:54

628 Views

You can use the tempfile module to create a unique temporary file in the most secure manner possible. There are no race conditions in the file’s creation. The file is readable and writable only by the creating user ID. Note that the user of mkstemp() is responsible for deleting the ... Read More

How to create a duplicate file of an existing file using Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 17-Feb-2020 12:49:55

888 Views

The shutil module provides functions for copying files, as well as entire folders.Calling shutil.copy(source, destination) will copy the file at the path source to the folder at the path destination. (Both source and destination are strings.) If destination is a filename, it will be used as the new name of ... Read More

How can I import modules for a Python Azure Function?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 17-Feb-2020 07:41:36

921 Views

As of writing this, Python support for Azure Functions is experimental. So right now there is no way to directly get a module from a package manager to be installed on your instance. You'll need to bring your own modules with code. No modules are available by default on Azure ... Read More

How to convert byte literals to python strings?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Feb-2020 10:32:41

380 Views

To convert byte literals to Python strings, you need to decode the bytes. It can be done using the decode method on the bytes object.  example>>> b"abcde".decode("utf-8") u'abcde'You can also map bytes to chr if the bytes represent ASCII encoding as follows −bytes = [112, 52, 52] print("".join(map(chr, bytes)))Outputp44Read More

How we can store Python functions in a Sqlite table?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Feb-2020 06:59:23

152 Views

In the following code, we import sqlite3 module and establish a database connection. We create a table and then insert data and retrieve information from the sqlite3 database and close the connection finally.Example#sqlitedemo.py import sqlite3 from employee import employee conn = sqlite3.connect('employee.db') c=conn.cursor() c.execute(‘’’CREATE TABLE employee(first text, last text, pay ... Read More

How to pass a json object as a parameter to a python function?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Feb-2020 06:58:27

8K+ Views

We have this code below which will the given json object as a parameter to a python function.exampleimport json json_string = '{"name":"Rupert", "age": 25, "desig":"developer"}' print type (json_string) def func(strng):     a =json.loads(strng)     print type(a)     for k, v in a.iteritems():           ... Read More

How to return a json object from a Python function?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 13-Feb-2020 06:57:35

1K+ Views

We return a json object from a python function as follows using given python dictionary.Exampleimport json a = {'name':'Sarah', 'age': 24, 'isEmployed': True } # a python dictionary def retjson(): python2json = json.dumps(a) print python2json retjson()Output{"age": 24, "isEmployed": true, "name": "Sarah"}

How to use the ‘except clause’ with No Exceptions in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Feb-2020 11:21:05

1K+ Views

If we define except clause with no exceptions, it can handle all types of exceptions. However, neither it’s a good coding practice nor it is recommended.Exampletry: print 'foo'+'qux'+ 7 except: print' There is error'OutputYou get the outputThere is errorThis type of Python try-except block can handle all types of exceptions, ... Read More

Explain Try, Except and Else statement in Python.

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Feb-2020 11:17:53

889 Views

The common method to handle exceptions in python is using the "try-except" block. We can even include an else clause after except clause. The statements in the else block are executed if there is no exception in the try statement.The optional else clause is executed if and when control flows ... Read More

How do I manually throw/raise an exception in Python?

Rajendra Dharmkar

Rajendra Dharmkar

Updated on 12-Feb-2020 11:04:46

165 Views

We use the most specific exception constructor that fits our specific issue rather than raise generic exceptions. To catch our specific exception, we'll have to catch all other more specific exceptions that subclass it.We should raise specific exceptions and handle the same specific exceptions.To raise the specific exceptions we use ... Read More

Advertisements