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.

  1. 10 ** 3

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

  1. #Check if it is int
  2. isinstance(variable, int)
  3. #Check if it is string
  4. isinstance(variable, basestring)

If you want to test that a dictionary is not empty

  1. bool(my_dict)

Check if a file exists

  1. import os
  2. os.path.isfile(path)

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

  1. import os
  2.  
  3. x = [os.path.join(r,file) for r,d,f in os.walk("E:") for file in f]
  4.  
  5. for val in x:
  6. print(val)