Python: Misc

(Last Updated On: )

This post will just be for misc basic python stuff.

Let’s say you want to raise 10 to the power of 3.

10 ** 3

If you want to check if a variable is of a specific type. You can use “isinstance”:

#Check if it is int
isinstance(variable, int)
#Check if it is  string
isinstance(variable, basestring)

If you want to test that a dictionary is not empty

bool(my_dict)

Check if a file exists

import os
os.path.isfile(path)

Recursively loop through a directory to get the files with path to a list

import os

x = [os.path.join(r,file) for r,d,f in os.walk("E:") for file in f]

for val in x:
    print(val)